|
|
|
|
Composite Pattern
A Composite is a
tree structure consisting of individual objects mixed with compositions
of objects, that is, objects that have other objects as their children.
The goal of the Composite pattern is to be able to treat individual
objects and compositions of objects the same way. All objects in the
Composite are derived from Composite itself.
The Gang Of Four (GoF)
defined the Composite pattern as follows in their most famous book
"Design Patterns”
Gamma et al., Addison-Wesley, ISBN:0-201-63361-2” – “Compose
objects into tree structures to represent whole-part hierarchies.
Composite lets clients treat individual objects and compositions of
objects uniformly."
The key to a
composite pattern is an abstract class that represents both primitives
(individual objects) and their containers (compositions of objects).
Define an abstract base class (Component) that specifies the behavior
that needs to be exercised uniformly across all primitive and composite
objects. Subclass the Primitive and Composite classes off of the
Component class. Each Composite object "couples" itself only to the
abstract type Component as it manages its "children". Non-software Example
Although the example
is abstract, arithmetic expressions are Composites. An arithmetic
expression consists of an operand, an operator (+ - * /), and another
operand. The operand can be a number, or another arithmetic expression.
Thus, 2 + 3 and (2 + 3) + (4 * 6) are both valid expressions. [Michael
Duell, "Non-software examples of software design patterns", Object
Magazine, Jul 97, p54]
The UML
representation of a Composite pattern is shown below.
In the above diagram Leaf and Composite are
type of component. The difference is that the Composite can contain
other components either other Composites or Leafs as child components.
But the Client treats both Leaf and Composite as just a Component. C# Implementation
|