โ† CS Visual Lab ยท Design Patterns

๐ŸŽ Decorator

๐Ÿ’ก The ideaAdd behaviour to an object by wrapping it in another object with the same interface. Wrappers can be stacked, instead of making many subclasses.
๐Ÿ  In real lifeA plain coffee, then add milk, then add sugar: each addition wraps the cup and adds to the price.

๐ŸŽฌ Watch it work

Step 1 of 4
Base component

A simple data source.

interface Source { write(data: string): void }
class FileSource implements Source { write(d) { /* save */ } }

โœ… Check yourself (2 of 3 to pass)

1. Decorator adds behaviour by:
2. A Java example of Decorator is:
3. Decorator is a โ€ฆ pattern.
Next: Singleton โ†’Get a free certificate in Design Patterns