Describe the concept of transparent enclosure ?
The concept of transparent enclosure combines the notions of (1) single-child (or single-component) composition and (2) compatible interfaces. Clients generally can’t tell whether they’re dealing with the component or its enclosure (i.e., the child’s parent), especially if the enclosure simply delegates all its operations to its component.
What is the intent of the Decorator pattern ?
Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
What is the motivation of the Decorator pattern ?
Sometimes we want to add responsibilities to individual objects, not to an entire class.
A graphical user interface toolkit, for example, should let you add properties like borders or behaviors like scrolling to any user interface component.
Why is inheritance not as appropriate as decorators are (relying on composition) ?
The Decorator pattern provides a more flexible way to add responsibilities to objects than can be had with static (multiple) inheritance.
With decorators, responsibilities can be added and removed at run-time simply by attaching and detaching them.
In contrast, inheritance requires creating a new class for each additional responsibility (e.g., BorderedScrollableTextView, BorderedTextView). This gives rise to many classes and increases the complexity of a system. Additionally A client can’t control at Runtime how and when to decorate the component with a border. The choice to decorate is made at compile time. Remove the decoration will require either recreating the object or make the class customizable, then more complex. This complexity increase with the number of decorations.
Furthermore, providing different Decorator classes for a specific Component class lets you mix and match responsibilities.
What is the applicability of the Decorator Pattern ?
Use Decorator :
What are the participants of the Decorator pattern structure ?
What is the role of the “component” in the Decorator pattern structure ?
– Defines the interface for objects that can have responsibilities added to them dynamically.
What is the role of the “Concrete Components” in the Decorator pattern structure ?
– defines an object to which additional responsibilities can be attached.
What is the role of the “Decorator Interface or Abstract Class” in the Decorator pattern structure ?
– maintains a reference to a Component object and defines an interface that conforms to Component’s interface.
What is the role of the “Concrete Decorators” in the Decorator pattern structure ?
– adds responsibilities to the component. Often by extending operations inherithed from the Decorator Interface and the component. Sometimes by exposing new operations. Or both.
What are the most important collaborations in the Decorator pattern ?
Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.
How the Decorator pattern is also known as ?
Wrapper
What is the structure of the Decorator pattern
What are the consequences of using the “Decorator pattern” ?