Structural design pattern: Decorator pattern


Decorator Design Pattern 

Decorator Design Pattern is a structural pattern that lets you dynamically add behavior to individual objects without changing other objects of the same class. It uses decorator classes to wrap concrete components, making functionality more flexible and reusable.

Key Components of the Decorator Design Pattern

  • Component Interface: Defines common operations for components and decorators.
  • Concrete Component: Core object with basic functionality.
  • Decorator: Abstract wrapper that holds a Component reference and adds behavior.
  • Concrete Decorator: Specific decorators that extend functionality of the component.

  • 1. Component Interface(Coffee)

  • public interface Coffee {
        String getDescription();
        double getCost();
    }
    
Concrete component.
public class PlainCoffee implements Coffee {
@Override 
public String getDescription()\
{ return "Plain Coffee"; }
@Override 
public double getCost() { return 2.0; }
}

ConcreteDecorators

public interface Coffee {
    String getDescription();
    double getCost();
}

//crete an abstract class implementing coffee
public abstract class CoffeeDecorator implements Coffee {
    protected Coffee decoratedCoffee;
    public CoffeeDecorator(Coffee decoratedCoffee) {
        this.decoratedCoffee = decoratedCoffee;
    }
    public String getDescription() {
        return decoratedCoffee.getDescription();
    }
    public double getCost() {
        return decoratedCoffee.getCost();
    }
}

//create decorator using abstract class.
public class MilkDecorator extends CoffeeDecorator {
    public MilkDecorator(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }
    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription() + ", Milk";
    }
    @Override
    public double getCost() {
        return decoratedCoffee.getCost() + 0.5;
    }
}

public class SugarDecorator extends CoffeeDecorator {
    public SugarDecorator(Coffee decoratedCoffee) {
        super(decoratedCoffee);
    }
    @Override
    public String getDescription() {
        return decoratedCoffee.getDescription() + ", Sugar";
    }
    @Override
    public double getCost() {
        return decoratedCoffee.getCost() + 0.2;
    }
}

public class Main {
    public static void main(String[] args) {
        // Plain Coffee
        Coffee coffee = new PlainCoffee();
        System.out.println("Description: " + coffee.getDescription());
        System.out.println("Cost: $" + coffee.getCost());

        // Coffee with Milk
        Coffee milkCoffee = new MilkDecorator(new PlainCoffee());
        System.out.println("\nDescription: " + milkCoffee.getDescription());
        System.out.println("Cost: $" + milkCoffee.getCost());

        // Coffee with Sugar and Milk
        Coffee sugarMilkCoffee = new SugarDecorator(new MilkDecorator(new PlainCoffee()));
        System.out.println("\nDescription: " + sugarMilkCoffee.getDescription());
        System.out.println("Cost: $" + sugarMilkCoffee.getCost());
    }
}
Description: Plain Coffee
Cost: $ 2

Description: Plain Coffee, Milk
Cost: $ 2.5

Description: Plain Coffee, Milk, Sugar
Cost: $ 2.7

Comments

Popular posts from this blog

Archunit test

Hexagonal Architecture

visitor design pattern