Applied Design Patterns with Java

Structural :: Composite (163) {C ch 11}


Collaborations

Consequences

The Composite pattern has the following consequences:

  1. Defines class hierarchies of primitive objects and Composite objects. Primitive objects can be composed into more complex objects, which can be composed recursively. Wherever client code expects a primitive object, it can also take a Composite object.
  2. Makes the client simple. Clients can treat Composite structures and individual objects uniformly. Clients shouldn't know or care if they're dealing with a leaf or a Composite component. This simplifies client code, because it avoids having to write tag-and-case-statement-style functions over the classes that define the composition.
  3. Makes it easier to add new kinds of components. Newly defined Composite or Leaf subclasses work automatically with existing structures and client code. Clients don't have to be changed for new Component classes.
  4. Can make a 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. The type system can't enforce those constraints. Run-time checks are used instead.

Catalog Structural Prev Next