Composite pattern 在你需要tree 的时候,会是选择之一。
The Composite Pattern allows you to compose object into a tree structure to represent the
part-whole hierarchy which means you can create a tree of objects that is made of different
parts.but that can be treated as a whole one big thing.Composite lets clients to treat individual
objects and compositions of objects uniformly,that is the intent of the Composite Pattern.
Composite Pattern consists of following objects:
Base Component: if the interface for all objects in the composition, client program uses
base component to work with the objects in the compositions, it can be an interface or an abstract class with some methods common to all the objects.
Leaf: Defines the behavior for the elements in the composition, it is the build block for the composition and implements base component, it does not have references to other components.
Composite: it consists of leaf elements and implements the operations in base component.
Usage in JDK:
java.awt.Container #add(Component) is a great example of Composite pattern in java and used
in a lot in Swing.
阅读(466) | 评论(0) | 转发(0) |