[design-patterns] What is a wrapper class?

What is a wrapper class?
How are such classes useful?

This question is related to design-patterns wrapper

The answer is


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


Java programming provides wrapper class for each primitive data types, to convert a primitive data types to correspond object of wrapper class.


Wrapper classes came in to existence to fulfill a basic need of programmers - i.e. to use primitive values wherever only Objects are allowed. As their name suggests wrapper classes wrap around a primitive value and hold the value in an Object. So, all those places where primitives were not allowed - such as generics, hashmap-keys, Arraylists etc - programmers now have an option of providing these primitive values as their corresponding wrapper types.

In addition these wrapper types have a number of utility methods for converting from primitive type to corresponding wrapper types and back, and also from Strings to wrapper types and back.

I have written a detailed article on wrapper classes in a my blog which explains the concept of wrapper types in depth - http://www.javabrahman.com/corejava/java-wrapper-classes-tutorial-with-examples/ (Disclosure - This blog is owned by me)


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.


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.


I currently used a wrapper class for my project and the main benefits I get (just a single benefit to widen the topic explanation):

Exception handling: My main class ,that another class wraps, has methods that are throwing exceptions if occurs any, so I created a wrapper class that handles the exceptions and logs them immediately. So, in my main scope, there is no exception handling. I just call a method and do something.

Easy Usage: I can easily initiate the object. Normally initiating phase is constructed of a lot of steps.

Code Readability: When another programmer opens my code, the code will seem very clear and easy to manipulate.

Hiding the Details: If you are generating a class that another programmer is going to use, then you can wrap the details like "error handling, exception handling, logging messages and etc..." so that the programmer will not be have to handle the chaos, just simply uses the methods.


Just 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


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.


Wrapper class is a wrapper around a primitive data type. It represents primitive data types in their corresponding class instances e.g. a boolean data type can be represented as a Boolean class instance. All of the primitive wrapper classes in Java are immutable i.e. once assigned a value to a wrapper class instance cannot be changed further.


A wrapper class is a class that serves the sole purpose of holding something and adding some functionality to it. In Java since the primitives (like ints,floats,chars...) are not objects so if you want to treat them like one then you have to use a wrapper class. Suppose you want to create a Vector of ints, the problem is a Vector only holds Objects not primitives. So what you will do is put all the ints in an Integer wrapper and use that. Example:

int number = 5;
Integer numberWrapped = new Integer(number);
//now you have the int in an object.
//and this is how to access the int value that is being wrapped.
int again = numberWrapped.intValue();

Wrapper classes provide a way to use primitive types as objects. For each primitive , we have a wrapper class such as,

int Integer
byte Byte 

Integer and Byte are the wrapper classes of primitive int and byte. There are times/restrictions when you need to use the primitives as objects so wrapper classes provide a mechanism called as boxing/unboxing.

Concept can be well understood by the following example as

double d = 135.0 d;
Double doubleWrapper = new Double(d);

int integerValue = doubleWrapper.intValue();
byte byteValue = doubleWrapper.byteValue();
string stringValue = doubleWrapper.stringValue();

so this is the way , we can use wrapper class type to convert into other primitive types as well. This type of conversion is used when you need to convert a primitive type to object and use them to get other primitives as well.Though for this approach , you need to write a big code . However, the same can be achieved with the simple casting technique as code snippet can be achieved as below

double d = 135.0;
int integerValue = (int) d ;

Though double value is explicitly converted to integer value also called as downcasting.


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 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 !


Wrapper class is a class that wrap the another class and provide the abstraction between client and the original class being wrapped.


To make a wrapper class well being is not a easy job. To understand a wrapper class how it is designed by some others is also not a easy job. Because it is idea, not code. Only when you understand the idea, you can understand wrapper.


a wrapper is class which is used to communicate between two different application between different platform