Decorator pattern implementation

The following two tabs change content below.
Prasad Kharkar is a java enthusiast and always keen to explore and learn java technologies. He is SCJP,OCPWCD, OCEJPAD and aspires to be java architect.

Latest posts by Prasad Kharkar (see all)

We will continue with our design patterns article series. This article will deal with another type of design pattern i.e. Decorator. As the name suggests, it is used to add some new functionality to existing objects. We decorate an object with new functionality. This article will provide decorator pattern implementation with an example.

Decorator Pattern Implementation:

Decorator pattern attaches additional responsibilities to an object. Decorators provide a flexible alternative to subclassing for extended functionalities.

Before decorator pattern implementation, let us understand class diagram.

DecoratorDesignPattern_classDiagram

  • Component: A component can be an object which is to be decorated as well as an object which decorates. The component to be decorated is wrapped by a component which decorates. Component can be interface or an abstract class
  • ConcreteComponent: An implementation of Component for which we are going to add new responsibilities. This is the component to be decorated. 
  • Decorator: A Decorator IS-A Component as well as a Decorator HAS-A Component. i.e. Decorator implements a Component as well as it has instance variable of Component. This is the class which serves as abstract class for all concrete decorators.
  • ConcreteDecorator: This can extend behavior or state of a Component. In above example, ConcreteDecoratorA adds newMethod() and ConcreteDecoratorB adds newMember.

Decorator Pattern Implementation:

We will take our favourite Car example for decorator pattern implementation.

  • We have an interface Car having getDetails() and calculateCost() abstract methods.
  • SportsCar implements Car and implements getDetails() and calculateCost()
  • AccessoriesDecorator implements Car and acts as abstract Decorator. It creates HAS-A relationship by having an instance variable of Car.
  • MusicSystem extends AccessoriesDecorator and adds new behaviour getMusicSystemDetails() which returns details about music system.
  • BumperSticker extends AccessoriesDecorator and adds new field color which holds color of BumperSticker.
Decorator example

Decorator example

Note that our aim is to decorate our SportsCar with MusicSystem and BumperSticker, but we also need to calculate total cost for it. MusicSystem and BumperSticker will have their own cost calculations.

For decorator pattern implementation, we will have to notice few things.

  • An object that is to be decorated is wrapped with an object that acts as decorator. So we can create a SportsCar and wrap it around a MusicSystem. To calculate total cost, we will call SportsCar calculateCost() method from within MusicSystem calculateCost();
  • Similar approach can be followed for BumperSticker also.

Decorator Pattern Implementation Code:

Car.java

SportsCar.java

AccessoriesDecorator.java

MusicSystem.java

BumperSticker.java

Now our decorator pattern implementation is done. We will execute code using our Dealer now.

Dealer.java

This produces output

Points to note here

  • We created ferrari, a SportsCar instance which is of type Car.
  • We created musicSystem, a MusicSystem instance which holds ferrari.
  • We created bumperSticker, a BumperSticker instance which holds musicSystem, which in turn holds ferrari.
  • Now, bumperSticker.getDetails() calls getDetails() from MusicSystem because we have wrapped a MusicSystem in BumperSticker by Car bumperSticker = new BumperSticker(musicSystem, "RED");
  • Now, getDetails() from MusicSystem calls getDetails from SportsCar because we have wrapped a SportsCar in MusicSystem by Car musicSystem = new MusicSystem(ferrari);
  • In similar fashion, calculateCost() from BumperSticker calls calculateCost from MusicSystem which in turn calls calculateCost from SportsCar.

This is decorator pattern implementation. In next article we will see how it is implemented in java EE

Share Button

Leave a Reply

Your email address will not be published. Required fields are marked *