Design patterns: Decorator

date: · 2 min
author: Mauto
series: Design Patterns/ part 10/24

The classic Gang of Four patterns, one per part, with practical PHP examples.

  1. Design Patterns
  2. Design patterns: Singleton
  3. Design patterns: Factory Method
  4. Design patterns: Abstract Factory
  5. Design patterns: Builder
  6. Design patterns: Prototype
  7. Design patterns: Adapter
  8. Bridge Pattern in PHP: What It Is, When to Use It, with Examples
  9. Design patterns: Composite
  10. Design patterns: Decorator
  11. Design patterns: Facade, What It Is and How to Use It (PHP)
  12. Design patterns: Flyweight
  13. Design patterns: Proxy
  14. Design patterns: Chain of Responsibility Explained (PHP)
  15. Design patterns: Command
  16. Design patterns: Interpreter
  17. Design patterns: Iterator
  18. Design patterns: Mediator
  19. Design patterns: Memento
  20. Design patterns: Observer
  21. Design patterns: State, What It Is and How to Use It (PHP)
  22. Design patterns: Strategy
  23. Design patterns: Template Method
  24. Design patterns: Visitor
the whole guide >

The Decorator pattern is a structural design pattern that lets you add behavior to an object dynamically.

Characteristics of the Decorator pattern

  1. Dynamic addition of behavior: The Decorator Pattern lets you add new behaviors and responsibilities to existing objects dynamically, without altering the original code.
  2. Flexible composition: It lets you combine different decorators to extend an object’s functionality, offering great flexibility when building complex behaviors.
  3. Follows the Open/Closed Principle: It follows the open/closed principle, letting you extend the functionality of objects without modifying existing code. You can add decorators without changing the original classes.
  4. Support for complex objects: It makes it easy to manage objects with many variations and options, letting you create complex configurations without needing a static class hierarchy.
  5. Unified interface: Decorated objects and original objects implement the same interface, so clients can use decorated and undecorated objects interchangeably, without worrying about implementation differences.

Example in PHP:

interface Coffee {
    public function cost();
}

class BasicCoffee implements Coffee {
    public function cost() {
        return 5;
    }
}

class MilkDecorator implements Coffee {
    private $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function cost() {
        return $this->coffee->cost() + 2; // Adds the cost of the milk
    }
}

class SugarDecorator implements Coffee {
    private $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function cost() {
        return $this->coffee->cost() + 1; // Adds the cost of the sugar
    }
}

// Usage
$basicCoffee = new BasicCoffee();
$milkCoffee = new MilkDecorator($basicCoffee);

echo $milkCoffee->cost() . "\n"; // 7 (5 + 2)

// Decorators can be combined: milk and sugar
$milkSugarCoffee = new SugarDecorator(new MilkDecorator(new BasicCoffee()));

echo $milkSugarCoffee->cost() . "\n"; // 8 (5 + 2 + 1)

BACK TO THE GUIDE

Copied