Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Design pattern that Wrapper Classes use in Java?

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?

like image 903
zack Avatar asked Nov 15 '18 08:11

zack


People also ask

What design patterns are wrappers?

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.

Which design pattern is used in Java?

Iterator pattern is widely used in Java Collection Framework where Iterator interface provides methods for traversing through a collection.

Where wrapper classes are used in Java?

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.

What are the methods of wrapper class?

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()


2 Answers

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

like image 198
user7294900 Avatar answered Oct 23 '22 12:10

user7294900


All of the three design patterns in someway describe a wrapper:

  • Decorator pattern. Wraps a component and potentially decorates it with some additional characteristics.
  • Adapter pattern. Simply wraps a component to provide a suitable interface for consumers.
  • Facade pattern. Wraps a component to facilitate the usage of otherwise complex external interface.
like image 23
NiVeR Avatar answered Oct 23 '22 11:10

NiVeR