Info

Adding behaviour without altering the class itself
Facilitate the addition of behaviour to individual objects

Motivation

  • Enhance the object with additional functionality
  • Don’t want to rewrite or alter existing code - OCP
  • Want to keep new function separate - SRP
  • Need to interact with existing structures

Implementation

Aggregate the decorated object

Inherit from the decorated object

Dynamic Decorator

  • keep the reference to the decorated object(s)
    • ColoredShape{Square{}}

Static Decorator

  • Use mixin inheritance
    • ColoredShape<Square>

Info

Both approaches allow limitless composition
TransparentShape<ColoredShape<Circle>>

Compare to Proxy