Decorator Pattern — get the gist in 2 min.

aditya chaudhari
JavaDeveloperDiary — JDD
3 min readJul 5, 2022
decorator pattern icecream example — img source — homestratosphere dotom

In simple words, the decorator pattern helps attach different behaviors to the object at the runtime by wrapping the object with a specific type of wrapper class which contains the new behavior logic.

It is a type of structural pattern where object composition is important to build a larger structure.

Think about the Decorator pattern when overriding a methods (behaviors) by using inheritance is not the solution for you but you would still need to override a behavior for that object at runtime.

Decorator pattern structure .

Simple Example :
Consider you want to design a solution for an icecreme vending machine.
where there is base icecreme cone and then there are many toppings offered like, chocolate , nuts , mint topping etc. each of these toppings has additional cost on top of the base variant. (link for complete github code)

decorator pattern — a component is wrapped in another using decorator patter.

In image above , observe an basic iceCreme is wrapped in ChocolateAddon decorator then its wrapped with MintAddon decorator and further as many wrapper we can add on top of it.
This is basic idea behind decorator pattern, which is to have ability to wrap object with wrapper and attach different behavior then objects original behavior logic.

Structure and different components in decorator pattern :
Component
: defines the interface for objects that can have responsibilities added to them dynamically.
It defines a common interface for the wrapper and wrapped objects.

ConcreteComponent: defines basic behavior.
It’s an object which can be altered using decorators.

Decorator: maintains a reference to a component object(wrapped object) and a base decorator for concrete decorators to implement.

ConcreteDecorator: adds responsibilities to the component dynamically at the runtime.

Client: can wrap multiple components in hierarchies where each component can be wrapped by another.

Usage of decorator pattern in java libraries / framework :
Remember a code like , “BufferedReader br =
new BufferedReader(new FileReader("here goes some filename "));" ?
BuffereedReader internally is based on Decorator pattern philosophy.

Also, in the spring framework, AOP (Aspect-oriented programming), transaction internally uses a decorator pattern.

The real fun is in the details, if you wish to know and understand the pros-cons, java-code example for the decorator pattern then please consider checking the detailed article at [decorator-pattern-explained-5-min-read]

Thanks for reading. Love IT, Live IT, Enjoy IT.

On the journey to understand the core essence of design patterns.
Every Tuesday I publish a small 2 min post and a long 5–10 min post along with a code example, please consider follow if you wish to get notified!

--

--

aditya chaudhari
JavaDeveloperDiary — JDD

building efficient, scalable and maintainable enterprise e-commerce applications using java, spring framework and SAP CC. Life Mantra → Love IT Live IT Enjoy IT