7/31/2011

Design Pattern - Composite

Definition
Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly. (include abstract class as a component of sub-class).

UML Class Diagram

Composite: when and where use it
- The Composite design pattern is an in-memory data structures with groups of objects, each of which contain individual items or other groups. A tree control is a good example of a Composite pattern. The nodes of the tree either contain an individual object (leaf node) or a group of objects (a subtree of nodes).  All nodes in the Composite pattern share a common interface which supports individual items as well as groups of items.
This common interface greatly facilitates the design and construction of recursive algorithms that iterate over each object in the Composite collection. 

- Fundamentally, the Composite pattern is a collection that you use to build trees and directed graphs. It is used like any other collection, such as, arrays, list, stacks, dictionaries, etc. 

Sample
- Component abstract class

- Component concrete class

- Main class


Ref: Gang of Four tutorial

No comments:

Post a Comment