LearningTech

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:
    def __init__(self):
        pass

    def execute(self):
        print('Executing Decorator')