Structural

Decorator

Description

Lets you attach new behaviors to objects by placing these objects inside special wrapper objects that contain the behaviors.

Where to use

  • Adding features dynamically.
  • \n
  • Streams (Java I/O).

Real World Example

A 'Coffee' object. You wrap it in 'MilkDecorator', then 'SugarDecorator'. The cost() method adds up existing cost + milk + sugar.

Code Example

class Decorator:\n    def __init__(self):\n        pass\n\n    def execute(self):\n        print('Executing Decorator')