Decorator Design Pattern
“linked list of features”
The decorator design pattern allows us to add new behaviors to objects dynamically by placing them inside special wrapper objects, called decorators.
When to use Decorator Pattern
Add / remove behaviour at run-time with a polymorphic class.
Resource: https://refactoring.guru/design-patterns/decorator/cpp/example#lang-features
Learned in CS247 - Software Engineering Principles.
Pieces of the decorator pattern:
- Abstract Component: Gives the interface for our classes
- Concrete Component: Implements the “basic” version of the interface
- Abstract Decorator: Organizes decorators and has-a decorator or the concrete component.
- Concrete Decorators: Implement the interface, call
operation()
on the next object in this linked list.
Template code for a Pizza example (UML below)
Example: