September 21, 2003

Structural Pattern – Composite Model

Compose objects into tree structures to represent part-whole hierarchies. This pattern lets a client treat a tree or a node of the tree in the same manner

• This is very common pattern where multiple objects are composed to give the appearance of one large object. Again, using our example of COM, COM containment and aggregation are examples of the composite pattern.

• In this pattern, one object composes other objects by including the composed objects as member variables in its class declaration.

• It then exposes the services of these composed objects in a unified manner, such that to a client of this composed object, it appears as though all the services provided by the composed object come from one single object.

NOTE: Composition not only allows you to build complex objects based on smaller objects, it also allows you to customize the calls to the composed objects from the outer object.

Example:

The XmlNode class in .NET is a composite

Links:

http://www.dofactory.com/Patterns/PatternComposite.aspx

Composite pattern discussion