Build fluency in the vocabulary of wrapping an object with layered wrappers that add behavior behind the same interface.
0 / 5 completed
1 / 5
At standup, a dev mentions wrapping an object in another object that implements the same interface, adding extra behavior around the original object's calls, and allowing several such wrappers to be layered on top of each other. What is this design pattern called?
The decorator pattern is exactly this: the decorator pattern wraps an object in another object that implements the same interface, adding extra behavior before or after delegating to the original object's calls, and allows several such wrappers to be layered on top of each other to combine behaviors. A hash collision is an unrelated hash-table concept about two keys sharing a bucket. This wrap-and-layer-behind-the-same-interface approach is exactly why the decorator pattern lets behaviors be combined at runtime without creating a new subclass for every combination.
2 / 5
During a design review, the team applies the decorator pattern to a component that needs several optional extra behaviors combined in different ways, specifically because layering wrappers behind the same interface avoids creating a separate subclass for every possible combination of behaviors. Which capability does this provide?
The decorator pattern here provides Composable extra behavior without a subclass explosion, since each optional behavior is its own wrapper implementing the same interface, and any combination can be assembled by layering wrappers instead of writing a dedicated subclass per combination. Writing a dedicated subclass for every possible combination of optional behaviors grows combinatorially as the number of behaviors increases. This layer-wrappers-behind-the-same-interface behavior is exactly why the decorator pattern is favored whenever several optional behaviors can combine in different ways.
3 / 5
In a code review, a dev notices a component with several optional extra behaviors defines a separate subclass for every combination of those behaviors, instead of layering small wrapper objects behind a common interface. What does this represent?
This is a missed decorator-pattern opportunity, since layering small wrapper objects behind a common interface would let any combination of behaviors be assembled at runtime, instead of a dedicated subclass being written for every combination. A cache eviction policy is an unrelated concept about discarded cache entries. This subclass-per-combination pattern is exactly the kind of combinatorial bloat a reviewer flags once several optional behaviors can combine in different ways.
4 / 5
An incident report shows the number of subclasses in a component grew unmanageably as new optional behaviors were added, because each new behavior required a new subclass for every existing combination, instead of layering wrapper objects behind a common interface. What practice would prevent this?
Introducing the decorator pattern replaces subclass-per-combination growth with composable wrappers that can be layered as needed. Continuing to define a new subclass for every combination of optional behaviors regardless of how many optional behaviors the component ends up supporting is exactly what caused the issue described in this incident. This decorator-based approach is the standard fix once the subclass count for a component's behavior combinations grows unmanageable.
5 / 5
During a PR review, a teammate asks why the team reaches for the decorator pattern instead of simply subclassing the component for each combination of optional behaviors it needs. What is the reasoning?
The decorator pattern lets each optional behavior be layered independently behind a common interface, so combinations are assembled at runtime without new subclasses, while subclassing is simpler for one fixed combination but grows combinatorially as more optional behaviors need to be supported. This is exactly why the decorator pattern is favored once several optional behaviors can combine in different ways, while subclassing remains simpler for one fixed, unchanging combination.