It compose objects into tree structures to represent part-whole hierarchies.
Example:
A drawing may be composed of graphic primitives, such as lines, circles, rectangles, text, and so on.
Problems Resolved
It lets clients treat individual objects and compositions of objects uniformly.
We often must manipulate composites exactly the same way we manipulate primitive objects.
Sample Code (Composite method)
public void draw() { // Iterate over the components for(int i=0; i < getComponentCount(); ++i) { // Obtain a reference to the component and invoke its draw method Component component = getComponent(i); component.draw(); } }
0 comments
Post a comment