What is a wrapper class?
How are such classes useful?
views:
5176answers:
8Just what it sounds like: a class that "wraps" the functionality of another class or API in a simpler or merely different API.
See: Adapter pattern, Facade pattern
A wrapper class is a class that is used to wrap another class to add a layer of indirection and abstraction between the client and the original class being wrapped.
A wrapper class is a class that "wraps" around something else, just like its name.
The more formal definition of it would be a class that implements the Adapter Pattern. This allows you to modify one set of APIs into a more usable, readable form. For example, in C#, if you want to use the native Windows API, it helps to wrap it into a class that conforms to the .NET design guidelines.
In general, a wrapper class is any class which "wraps" or "encapsulates" the functionality of another class or component. These are useful by providing a level of abstraction from the implementation of the underlying class or component; for example, wrapper classes that wrap COM components can manage the process of invoking the COM component without bothering the calling code with it. They can also simplify the use of the underlying object by reducing the number interface points involved; frequently, this makes for more secure use of underlying components.
A wrapper class doesn't necessarily need to wrap another class. It might be a API class wrapping functionality in e.g. a dll file.
For example it might be very useful to create a dll wrapper class, which takes care of all dll initialization and cleanup and create class methods that wrap function pointers created from e.g. GetProcAddress()
Cheers !
a wrapper class is usually a class that has an object as a private property. the wrapper implements that private object's API and so it can be passed as an argument where the private object would.
say you have a collection, and you want to use some sort of translation when objects are added to it - you write a wrapper class that has all the collection's methods. when add() is called, the wrapper translate the arguments instead of just passing them into the private collection.
the wrapper can be used anyplace a collection can be used, and the private object can still have other objects referring to it and reading it.
It might also be valuable to note that in some environments, much of what wrapper classes might do is being replaced by aspects.
EDIT:
In general a wrapper is going to expand on what the wrappee does, without being concerned about the implementation of the wrappee, otherwise there's no point of wrapping versus extending the wrapped class. A typical example is to add timing information or logging functionality around some other service interface, as opposed to adding it to every implementation of that interface.
This then ends up being a typical example for Aspect programming. Rather than going through an interface function by function and adding boilerplate logging, in aspect programming you define a pointcut, which is a kind of regular expression for methods, and then declare methods that you want to have executed before, after or around all methods matching the pointcut. Its probably fair to say that aspect programming is a kind of use of the Decorator pattern, which wrapper classes can also be used for, but that both technologies have other uses.
There are several design patterns that can be called wrapper classes.
See my answer to "How do the Proxy, Decorator, Adaptor, and Bridge Patterns differ?"