Architectural Patterns
上QQ阅读APP看书,第一时间看更新

Composite

Composite objects let clients treat inpidual objects and composition of objects uniformly. Composite represents the hierarchies of objects as tree structures.

The preceding diagram depicts the standard structure of the Composite pattern and an implementation of a part-whole hierarchy (employee part of agent, Accountant, and teller), and to the Client, all objects are Composite and structured uniformly.

These are the benefits:

  • It simplifies the client code by hiding the complex communications (leaf or composite component)
  • It is easier to add new components, and client does not need a change when new components get added

The impact is such that it makes the design overly general and open as there are no restrictions to add any new components to composite classes.