The Composite Pattern Flashcards

1
Q

What is the intent of the composite pattern ?

A

Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

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

What is the motivation of the composite pattern ?

A

Avoid code complexity because of distinguishing classes of similar and composed objects on which the same operations are applied (on individual objects and on their composition).

The key to the Composite pattern is an abstract class that represents both primitives and their containers.

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

What is the applicability of the composite pattern ?

A

You want to represent part-whole hierarchies of objects.

You want clients to be able to ignore the difference between compositions of objects and individual objects. Clients will treat all objects in the composite structure uniformly.

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

What are the participants of the composite pattern ?

A
  • Component
  • Composite
  • Leaf
  • Clients
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the role of the ‘component’ participant in the composite model ?

A

– declares the interface for objects in the composition.
– implements default behavior for the interface common to all classes, as appropriate.
– declares an interface for accessing and managing its child components.
– (optional) defines an interface for accessing a component’s parent in the recursive structure, and implements it if that’s appropriate.

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

What is the role of the ‘leaf’ participant in the composite model ?

A

– represents leaf objects in the composition. A leaf has no children.

– defines behavior for primitive objects in the composition.

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

What is the role of the ‘composite’ participant in the composite model ?

A

– defines behavior for components having children.
– stores child components.
– implements child-related operations in the Component interface.

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

What is the role of the ‘client’ participant in the composite model ?

A

– manipulates objects in the composition through the Component interface.

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

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

A
  • Defines class hierarchies consisting of primitive objects and composite objects.
  • Makes the client simple.
  • makes it easier to add new kinds of components.
  • can make your design overly general: The disadvantage of making it easy to add new components is that it makes it harder to restrict the components of a composite. Sometimes you want a composite to have only certain components. With Composite, you can’t rely on the type system to enforce those constraints for you. You’ll have to use run-time checks instead
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Describe the collaborations between the composite pattern participants ?

A

Clients use the Component class interface to interact with objects in the composite structure. If the recipient is a Leaf, then the request is handled directly. If the recipient is a Composite, then it usually forwards requests to its child components, possibly performing additional operations before and/or after forwarding.

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

What are the questions/tradeoff to answer during implementation of the composite pattern ?

A
  1. Explicit parent references.
  2. Sharing components.
  3. Maximizing the Component interface.
  4. Declaring the child management operations.
  5. Should Component implement a list of Components ?
  6. Child ordering.
  7. Caching to improve performance.
  8. Who should delete component ?
  9. What’s the best data structure for storing componen
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the composite related patterns ?

A

Often the component-parent link is used for a Chain of Responsibility (223).

Decorator (175) is often used with Composite.

When decorators and composites are used together, they will usually have a common parent class.

So decorators will have to support the Component interface with operations like Add, Remove, and GetChild.

Flyweight (195) lets you share components, but they can no longer refer to their parents. Iterator (257) can be used to traverse composites.

Visitor (331) localizes operations and behavior that would otherwise be distributed across Composite and Leaf classes.

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