Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

So any idea what do they mean by it;

e.g. a simple question; we got two types of array sorting techniques, lets create a wrapper for it

The above one is a very simple example

like image 577
Basit Anwer Avatar asked Jul 20 '10 19:07

Basit Anwer


People also ask

What does a wrapper mean in programming?

In the context of software engineering, a wrapper is defined as an entity that encapsulates and hides the underlying complexity of another entity by means of well-defined interfaces.

What are wrapper used for?

Wrappers are used for two primary purposes: to convert data to a compatible format or to hide the complexity of the underlying entity using abstraction. Examples include object wrappers, function wrappers, and driver wrappers.

What does code wrapping mean?

Wrapping is the process of hiding PL/SQL source code. Wrapping helps to protect your source code by making it more difficult for others to view it. You can wrap PL/SQL source code with either the wrap utility or DBMS_DDL subprograms. The wrap utility wraps a single source file, such as a SQL*Plus script.

What is a wrapper in web development?

In programming languages such as JavaScript, a wrapper is a function that is intended to call one or more other functions, sometimes purely for convenience, and sometimes adapting them to do a slightly different task in the process. For example, SDK Libraries for AWS are examples of wrappers.


3 Answers

The term 'wrapper' gets thrown around a lot. Generally its used to describe a class which contains an instance of another class, but which does not directly expose that instance. The wrapper's main purpose is to provide a 'different' way to use wrapped object (perhaps the wrapper provides a simpler interface, or adds some functionality).

The word 'wrapper' can also be used when describing classic design patterns.

Wrapping an object to provide a simplified interface to it is often described as the 'Facade' pattern. The wrapper is the facade.

Sometimes you may have a class which would suit a specific interface, but you can't change the code for it to make it conform to that interface. You can create a wrapper for that class which implements the interface, but which directs most of the calls to the wrapped object. This is the 'Adapter' pattern. The wrapper is the adapter.

The instance you describe, where you have two classes which can do array sorting using different algorithms sounds like the 'Strategy' pattern, where you provide a way to perform an operation on some object, but the algorithm used for that operation may be different depending upon the structure of that object.

For example, one of your sort algorithms may be great for arrays of length less than 100, but then performance might be an issue for it. The other algorithm might be good for bigger arrays. You can create a 'wrapper' for the two algorithms which supports the sort operation but decides which algorithm to use based on the array length.

The vast majority of wrappers exist to hide some sort of complexity.

like image 187
Alex Humphrey Avatar answered Oct 19 '22 03:10

Alex Humphrey


Simple Explanation by Analogy

Example 1: An Ipad "wraps" an Iphone

An iphone is the only thing inside an Ipad. The Ipad literally wraps the Iphone. Except you can't make calls with an Ipad because the ability to make phone calls have not been "exposed". Apple want you to buy x2 products not just one :P

The Ipad is basically a large Iphone.....it's basically an Iphone but with a different exterior (larger screen). i.e. the Ipad has a different "wrapper".

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

"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.

In an automatic car, there is an 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. Leave all the complicated bits inside the wrapper, ensuring that those complicated bits are not 'exposed'.

Examples - wrappers used in code

  1. Pysch: It's a ruby wrapper around libyaml, which I believe is written in C. If you wanted to use a libyaml, but with ruby, what are your options? Pysch allows you to write in Ruby, while getting all the benefits of libyaml underneath. It "wraps" libyaml.

  2. .net wrappers: 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. The AutoCAD .net API is the perfect example. It is a glorified wrapper for the ObjectARX API.

like image 39
BenKoshy Avatar answered Oct 19 '22 05:10

BenKoshy


It's just that you write some easy to use code to hide the complexities of the code underneath. It's especially useful when you need to call some low level API from a higher level language and you want to keep all the "ugly" code hidden away.

Edit: You can find more about it in these two wiki articles: library and function

like image 27
Hans Olsson Avatar answered Oct 19 '22 05:10

Hans Olsson