Structural Patterns
Beverage
) and concrete implementations (Espresso
, Tea
). Instead of subclassing
every possible combination (e.g. EspressoWithMilk
), you create Decorator classes that wrap a Beverage
and add behavior
(e.g. MilkDecorator
). Multiple decorators can wrap an object at runtime to accumulate behaviors. Each decorator has the
same interface, so it’s transparent to clients.
ConcreteComponent
is wrapped by decorators. ConcreteDecoratorA
and ConcreteDecoratorB
extend the behavior of
ConcreteComponent
without modifying its code. The client_code function shows how a component and its decorated versions behave.