Reputation: 233
I have found an old post which does not clarify my understanding about the design patterns that are used by Wrapper Classes, Moreover, on reading from Wikipedia I'm not getting any clear information.
Does a Wrapper Class really use any design pattern or not?
If it is using a pattern, then which pattern is it out of these: Decorator Pattern
, Facade Pattern
or Adapter Pattern
?
Upvotes: 20
Views: 21932
Reputation: 17066
Wrapper classes use composition. The same composition as in the popular maxim, "Favor composition over inheritance." Composition is not a design pattern; however, most OO design patterns use composition as part of their implementation. This is one reason many people struggle to discriminate among different design patterns: the common use of composition makes them all appear the same to a certain degree.
There are two basic parts in a composition relationship: the composer and the composed. You can think of this generally as a part/whole relationship. It may be one-to-one or one-to-many. A wrapper is the composer, i.e. it is the whole. It may wrap one or more composed parts.
Many different design patterns make use of the general composition relationship for different purposes. Many of those different patterns are referred to collectively as "wrappers". The GoF book calls out at least two such patterns.
ADAPTER Also Known As Wrapper
page 139DECORATOR Also Known As Wrapper
page 175
In summary, Wrapper is not any single design pattern; rather, it is a category of design patterns. Incidentally, we see the same dynamic with the term Factory. There is no single design pattern named Factory; rather, it is a category of design patterns.
Upvotes: 7
Reputation: 5353
Well, the answers seems like to indicate that you can wrap an object for many reasons and as such, many patterns. So I'll try to give a more general answer.
A Wrapper is basically an object whose sole purpose is to provide something without modifying the main object (add fonctionnalities, simplify API, serialisation, ... see other answers), that wrapper is generally tightly coupled to the "main" object. For exemples look others answers.
Another alternative for some usage of the wrapper is inheritance, but not for every case.
As such the wrapper is just a technical way of doing some stuff. It is not a pattern in itself.
Upvotes: 3
Reputation: 58782
If you refer to wrapping primitive
Wrapper classes provide a way to use primitive types as objects
Adapter pattern is the most exact meaning:
A decorator makes it possible to add or alter behavior of an interface at run-time. Alternatively, the adapter can be used when the wrapper must respect a particular interface and must support polymorphic behavior, and the Facade when an easier or simpler interface to an underlying object is desired
We use Wrapper class ability to use primitive as Objects, meaning add support to a polymorphic behavior
Upvotes: 13
Reputation: 44150
They don't follow any of the design patterns that you have mentioned.
Adapter converts the interface of a class to another interface. Primitives do not implement any interfaces.
Decorator adds behaviour of to a class implementing one interface by wrapping it in another that implements the same interface. Primitives do not implement any interfaces.
A facade's purpose is to mask the complex behaviour of the object that it's wrapping. There is nothing less complex in a programming language than a primitive. The clue's in the name. If anything, the wrapper classes are the opposite of this.
Off the top of my here, here's a few design patterns that they do make use of:
Integer
, Long
and Byte
make use of an object pool of flyweight objects, to avoid creating unnecessary instances.
Boolean
somewhat tries to be a multiton (in that the constructor is deprecated) but in practice it isn't.
Upvotes: 4
Reputation: 9796
All of the three design patterns in someway describe a wrapper:
Upvotes: 10