Part 1 of 6 · 1 min read
Why this page comes fifth
In this module you will
- State the Decorator pattern and explain why it wraps an object with the same interface.
- Build priced extras for a booking by stacking decorators.
- Show why subclassing for every combination fails.
- Recognise decorators in Java: I/O streams, and a logging wrapper around a repository.
- Name the costs: many small objects and no reliance on object identity.
Level 1, module 4 showed that inheritance is a weak way to add behaviour, and that composition is stronger. Decorator is composition used to add behaviour at run time, so it is the pattern interviewers expect when features combine freely, such as pizza toppings, coffee extras or booking add-ons. The sources are "Design Patterns" (GoF) chapter 4, "Head First Design Patterns" (HF) chapter 3, and "Effective Java" (EJ) Item 18. Code was compiled and run before it was written here. Numbers marked "made up" only show the idea.