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
?
In software engineering, the adapter pattern is a software design pattern (also known as wrapper, an alternative naming shared with the decorator pattern) that allows the interface of an existing class to be used as another interface.
Iterator pattern is widely used in Java Collection Framework where Iterator interface provides methods for traversing through a collection.
The wrapper classes in java can be used when it is required to use the primitive types as objects. Moreover, wrapper classes also contain methods that unwrap the object and return the data type. The classes only handle objects in java. util package.
Every number type Wrapper class( Byte, Short, Integer, Long, Float, Double) contains the following 6 methods to get primitive for the given Wrapper object: public byte byteValue() public short shortValue() public int intValue()
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
All of the three design patterns in someway describe a wrapper:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With