Where and how is the term used WRAPPER in programming, what does it help to do?

前端 未结 7 1016
醉话见心
醉话见心 2021-01-29 18:07

I come across software developers using the term of creating Wrappers of other classes or APIs or even some codes, this is a term used by experienced Software Programmers

<
7条回答
  •  孤独总比滥情好
    2021-01-29 18:16

    Simple Explanation by Analogy: What is wrapping?

    Code not used to explain.

    Example 1: An Ipad "wraps" an Iphone

    What does the ipad do that the iphone doesn't do?

    Aren't they basically the same thing, except one has a bigger screen?

    The Ipad "wraps" the iphone: meaning that you have all the benefits of the iphone WITHIN the ipad itself, AND have the benefits of a larger screen etc. But fundamentally, inside the ipad, lies an Iphone.

    What then is different? The only thing that is different is the exterior: you have a larger screen, and perhaps, you cannot make phone calls on the Ipad.

    It's literally the same thing with "objects".

    .............the inside has all the good stuff which is the same, but a different exterior. A different wrapper.

    "Wrappers may expose different features than the underlying object" ......okay, but what does this mean?

    Sometimes the wrapper might limit the things you can access inside. For example, the ipad may limit your ability to make phone calls, even though the Iphone buried within it, has that capability.

    Example 2: Automatic car acting as a wrapper to a manual car

    Think of an automatic car, and a manual car. Behind the scenes, in an automatic car, there is some engineering mechanism which automatically changes the gears for you, but fundamentally, under the surface, the car is still a manual car. In other words, the automatic features of the car "wraps" the manual functionality of the car. If you wanted to manually change the gears by yourself in an automatic car - you simply can't do it. The ability to change gears "is not exposed" in the automatic car. But it is exposed in a manual car. Granted the analogy is a little strained, but I hope you see what I'm getting at.

    But what is the purpose of a wrapper?

    You'd write a wrapping class if you wanted to simplify things. You'd create an easy to use 'wrapper' and leave all the complicated bits inside the wrapper, ensuring that those complicated bits are not 'exposed'.

    For example, you could have a .net wrapper, which underneath, makes COM calls. If you didn't have a wrapper, then you'd be forced to make those COM calls yourself. Luckily with the wrapper, you'd just make the calls to the .net wrapping code - which would in turn make those COM calls. And that should hopefully simplify things for you.

提交回复
热议问题