CodeBlanks Logo Icon CodeBlanks

The Decorator pattern

PHP course · Advanced · Lesson 85/92

Learn the Decorator pattern that adds behavior without inheritance.

What does the Decorator pattern do?

The Decorator pattern adds behavior to an object without inheritance. You enrich behavior by wrapping the base class with decorators, leaving the base class untouched.

All pieces implement a common Component interface. ConcreteComponent carries the base behavior; a Decorator class holds a Component instance inside and delegates work to it. The decorator behaves like a component but customizes the behavior.

Adding behavior with ConcreteDecorator

A concrete decorator extends the Decorator class and overrides the operation method. It usually calls parent::operation() and modifies the result, adding a new piece on top of the wrapped component's behavior.

For example, ConcreteDecorator can append '+sign' to the inner component's 'hello' output. The inner component stays the same; the decorator wraps it and enriches the output.

Exercise Add behavior with the Decorator

Write a Component interface (with an operation(): string method). ConcreteComponent returns 'hello'. The Decorator class implements the Component interface and holds the inner component in its constructor. ConcreteDecorator extends Decorator; its operation returns parent::operation() . '+sign'. Call the wrapped chain and print the result.

Show example solution
string; }. ConcreteComponent returns 'hello'. Decorator implements Component, holds Component $inner in constructor, operation calls the inner. ConcreteDecorator extends Decorator, operation returns parent::operation() . '+sign'.
Example solution:
<?php
interface Component {
    public function operation(): string;
}
class ConcreteComponent implements Component {
    public function operation(): string { return 'hello'; }
}
class Decorator implements Component {
    public function __construct(private Component $inner) {}
    public function operation(): string { return $this->inner->operation(); }
}
class ConcreteDecorator extends Decorator {
    public function operation(): string { return parent::operation() . '+sign'; }
}
$c = new ConcreteDecorator(new ConcreteComponent());
echo $c->operation();

Test yourself

  1. The interface the Decorator implements

    <?php
    class Decorator implements ____ {
        public function __construct(private Component $inner) {}
        public function operation(): string { return $this->inner->operation(); }
    }
    
    Show answer
    <?php
    class Decorator implements Component {
        public function __construct(private Component $inner) {}
        public function operation(): string { return $this->inner->operation(); }
    }
    
  2. Match the Decorator concepts

    Component
    common interface
    ConcreteComponent
    base behavior
    Decorator
    the wrapper
    operation
    implements behavior
  3. What does the Decorator pattern do?

    • It deletes the class from memory
    • It adds behavior without inheritance
    • It guarantees a single instance
    • It adapts to an external interface
    Show answer

    It adds behavior without inheritance — Correct. Decorator adds behavior without inheritance.

All lessons in Advanced