The Decorator Pattern Flashcards

1
Q

Describe the concept of transparent enclosure ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is the intent of the Decorator pattern ?

A

Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the motivation of the Decorator pattern ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Why is inheritance not as appropriate as decorators are (relying on composition) ?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the applicability of the Decorator Pattern ?

A

Use Decorator :

  • to add responsibilities to individual objects dynamically and transparently, that is, without affecting other objects.
  • for responsibilities that can be withdrawn.
  • when extension by subclassing is impractical. Sometimes a large number of independent extensions are possible and would produce an explosion of subclasses to support every combination. Or a class definition may be hidden or otherwise unavailable for subclassing.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What are the participants of the Decorator pattern structure ?

A
  • the component interface
  • the concrete components classes
  • the Decorator Interface/abstract class
  • the concrete Decorator classes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the role of the “component” in the Decorator pattern structure ?

A

– Defines the interface for objects that can have responsibilities added to them dynamically.

  • It is the interface that should be Common between the Decorator (wrapper, enclosing) and the Decorated objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the role of the “Concrete Components” in the Decorator pattern structure ?

A

– defines an object to which additional responsibilities can be attached.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What is the role of the “Decorator Interface or Abstract Class” in the Decorator pattern structure ?

A

– maintains a reference to a Component object and defines an interface that conforms to Component’s interface.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the role of the “Concrete Decorators” in the Decorator pattern structure ?

A

– adds responsibilities to the component. Often by extending operations inherithed from the Decorator Interface and the component. Sometimes by exposing new operations. Or both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are the most important collaborations in the Decorator pattern ?

A

Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

How the Decorator pattern is also known as ?

A

Wrapper

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the structure of the Decorator pattern

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What are the consequences of using the “Decorator pattern” ?

A
  1. More flexibility than static inheritance
  2. Avoids feature-laden classes high up in the hierarchy: Decorator offers a pay-as-you-go approach to adding responsibilities.
  3. A decorator and its component aren’t identical. A decorator acts as a transparent enclosure. But from an object identity point of view, a decorated component is not identical to the component itself.
  4. Lots of little objects.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly