Wednesday, August 2, 2023

Decorator Design Pattern

Decorator Design Pattern in Java

The Decorator design pattern is a structural pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class. It is used when we want to add new functionality to an object without modifying its structure.

The pattern involves creating a decorator class, which wraps the original object and provides additional functionality by dynamically adding new behavior to it. The decorator class has the same interface as the original object, allowing it to be used interchangeably with the original object.

The Decorator pattern consists of four main components:

  1. Component: This is the interface that defines the methods that will be implemented by the concrete components and decorators.
  1. Concrete Component: This is the class that provides the basic implementation of the component interface.
  1. Decorator: This is the abstract class that implements the component interface and contains a reference to the component object. The decorator class has a constructor that takes a component object as an argument, and it can add new behavior to the component by overriding its methods.
  1. Concrete Decorator: This is the class that extends the decorator class and adds new behavior to the component.

The Decorator pattern is useful when we want to add new functionality to an object at runtime, without modifying its structure. It allows us to mix and match different behaviors dynamically, providing greater flexibility and modularity to the code.


//Component interface

interface Pizza {

 public String getDescription();

 public double getCost();


//Concrete component

class BasicPizza implements Pizza {

 public String getDescription() {

     return "Pizza with tomato sauce and cheese";


 public double getCost() {

     return 10.00;




abstract class ToppingDecorator implements Pizza {

 protected Pizza decoratedPizza;

 public ToppingDecorator(Pizza pizza) {

     decoratedPizza = pizza;


 public String getDescription() {

     return decoratedPizza.getDescription();


 public double getCost() {

     return decoratedPizza.getCost();



//Concrete decorator

class Pepperoni extends ToppingDecorator {

 public Pepperoni(Pizza pizza) {



 public String getDescription() {

     return decoratedPizza.getDescription() + ", pepperoni";


 public double getCost() {

     return decoratedPizza.getCost() + 2.50;



//Client code

public class DecoratorPizzaShop {

 public static void main(String[] args) {

     Pizza basicPizza = new BasicPizza();

     System.out.println("Basic pizza: " + basicPizza.getDescription() + " - Cost: $" + basicPizza.getCost());

     Pizza pepperoniPizza = new Pepperoni(basicPizza);

     System.out.println("Pepperoni pizza: " + pepperoniPizza.getDescription() + " - Cost: $" + pepperoniPizza.getCost());




Basic pizza: Pizza with tomato sauce and cheese - Cost: $10.0

Pepperoni pizza: Pizza with tomato sauce and cheese, pepperoni - Cost: $12.5

In this example, we have a Pizza interface that defines the methods for getting the description and 
cost of a pizza. 

The BasicPizza class is a concrete implementation of the Pizza interface.

The ToppingDecorator abstract class is a decorator that implements the Pizza interface and contains 
an instance of the Pizza interface. 

The Pepperoni class is a concrete decorator that extends the ToppingDecorator class 
and adds a pepperoni topping to the pizza.

In the PizzaShop class, we create a basic pizza and a pepperoni pizza using the decorator pattern. 
We first create a BasicPizza object, and then we create a Pepperoni object that takes the BasicPizza object as a parameter in its constructor. 

This creates a pizza with tomato sauce, cheese, and pepperoni. We then print out the descriptions 
and costs of both pizzas.

No comments:

Post a Comment

Create a Digital Clock using HTML and JavaScript

Create a Digital Clock using HTML and JavaScript  <! DOCTYPE html> < html > < head > <...


Search This Blog

Popular Posts