Decorator Design Pattern

Tushar Ghosh
2 min readFeb 27, 2023

--

UML of decorator design pattern

The Decorator Design Pattern is a structural design pattern that allow to add functionality to a object at runtime. At the same time, other object of the same class will not be affected.

In other words, the decorator pattern allows you to add or remove behavior from an object at runtime by wrapping it in one or more decorator objects.

The one of the benefits of the decorator design pattern is adding functionalities and modify the behavior of the objects.

We can implement the UML of decorator design pattern using java. The interface or abstract class defining the methods that will be implemented.

// Interface
public interface IItem {
public String getDescription();
public Double getCost();
}

// 🍔 this is the burger item class
public class Burger implements IItem {
public String getDescription() {
return "Basic burger";
}
public Double getCost() {
return 2.50;
}
}

Decorator class implements the interface and it has a HAS-A relationship with the interface.

public class ItemDecorator implements IItem {

public IItem item ;

public ItemDecorator(IItem foodItem) {
item = foodItem;
}

public String getDescription() {
return this.item.getDescription();
}

public Double getCost() {
return this.item.getCost();
}

}



public class CheeseDecorator extends ItemDecorator {

public CheeseDecorator(IItem foodItem) {
super(foodItem);
}

public String getDescription() {
return this.item.getDescription() +", cheese";
}

public Double getCost() {
return this.item.getCost() + .7;
}
}



public class BaconDecorator extends ItemDecorator {

public BaconDecorator(IItem foodItem) {
super(foodItem);
}

public String getDescription() {
return this.item.getDescription() + ", bacon";
}

public Double getCost() {
return this.item.getCost() + 1.0;
}
}

Here is the main method.

public class Main {

public static void main(String[] args) {
// create a basic burger
IItem burger = new Burger();
// add cheese
burger = new CheeseDecorator(burger);
// add bacon
burger = new BaconDecorator(burger);

// print the description and cost of the fully loaded burger
System.out.println("Description: " + burger.getDescription());
System.out.println("Total cost: $" + burger.getCost());

// Output
// Description: Basic burger, cheese, bacon
// Total cost: $4.2

}
}

In main method, we have created burger object, later add the cheese and bacon in the run time. When print the costing its showing the total cost of basic burger, cheese and bacon. How decorator pattern calling inside and calculate the price.

How decorator design pattern work

From the above figure, firstly “burger.getCost()” call the BaconDecorator, then CheeseDecorator, after that Burger class. Then getCost of Burger return the cost, after that, CheeseDecorator return cost and finally return the BaconDecorator. Add the total cost. Its works like a recursion.

References:

--

--

Tushar Ghosh
Tushar Ghosh

Written by Tushar Ghosh

MEAN | JavaScript | Node.js | React| Angular | Frontend

No responses yet