Documentos de Académico
Documentos de Profesional
Documentos de Cultura
- Pattlet:
The intent of this pattern is to compose objects into tree structures to represent part-whole hierarchies allowing clients to access both the individual objects and compositions in a uniform manner.
- Context
To manipulate a hierarchical collection of primitive and composite objects; To ignore the difference between compositions of objects and individual objects. Group of objects are to be treated in the same way as a single instance of an object.
Forces:
Data complexity: Reduce the complexity of the part-whole hierarchy by minimizing the number of different kinds of child objects that those in the tree need to be aware of; Uniformity: All objects, primitive or composite are treated the same way;
- Solution
Component
declares the interface for objects in the composition. implements default behavior for the interface common to all classes, as appropriate. declares an interface for accessing and managing its child components. (optional) defines an interface for accessing a component's parent in the recursive structure, and implements it if appropriate.
Leaf
represents leaf objects in the composition. A leaf has no children. defines behavior for primitive objects in the composition.
Composite
defines behavior for components having children. stores child components. implements child-related operations in the Component interface.
- Known uses
For example a program that manipulates a file system. A file system is a tree structure that contains Branches, which are Folders, as well as Leaf nodes which are Files. Note that a folder object usually contains one or more file or folder objects and thus is a complex object while a file
is a simple object. Note also that since files and folders have many operations and attributes in common, such as moving and copying a file or a folder, listing file or folder attributes such as file name and size, it would be easier and more convenient to treat both file and folder objects uniformly by defining a File System Element Interface. Also in graphics editors, where a shape can be basic or complex. An example of a simple shape is a line, whereas a complex shape is a rectangle which is made of four line objects. Since shapes have many operations in common such as rendering the shape to screen, and since shapes follow a part-whole hierarchy, composite pattern can be used to enable the program to deal with all shapes uniformly.
- Variants
The pattern also involves including the child-manipulation methods in the main Component interface, not just the Composite subclass. More recent descriptions sometimes omit these methods
- Related patterns
Decorator: Decorator is often used with Composite. When decorators and composites are used together, they will usually have a common parent class. So decorators will have to support the Component interface with operations like Add, Remove, and GetChild. Chain of Responsibility: The Chain of Responsibility and Composite patterns can be combined by adding parent links, so children can get information from an ancestor without knowing which ancestor.
Flyweight: Flyweight is often combined with Composite to implement shared leaf nodes.
- References
http://sergiotaborda.wordpress.com/desenvolvimento-de-software/java/patterns/composite/ http://www.oodesign.com/composite-pattern.html http://www.codeproject.com/Articles/185797/Composite-Design-Pattern http://sourcemaking.com/design_patterns/composite http://www.dofactory.com/Patterns/PatternComposite.aspx http://www.javaworld.com/javaworld/jw-09-2002/jw-0913-designpatterns.html http://www.pg.cefetpr.br/coinf/simone/patterns/composite.php
- Code Examples
Java http://www.oodesign.com/composite-pattern-shapes-example-java-sourcecode.html http://sourcemaking.com/design_patterns/composite/java/3 http://sourcemaking.com/design_patterns/composite/java/2 http://sourcemaking.com/design_patterns/composite/cpp/1 http://sourcemaking.com/design_patterns/composite/cpp/2 http://sourcemaking.com/design_patterns/composite/php
C++
PHP
- Quiz
1. Which of the following is not a participant in the GOF Composite design pattern?
Component Leaf Composite Compound
Singleton Observer