One, intent
combines objects into a tree structure to represent a "partial-whole" hierarchy. Composite makes users consistent with the use of individual objects and grouped objects. The
Composite change is the structure and composition of an object.
II, primary role in synthetic mode
abstract Component (Component) role: abstract role, which provides an interface to the objects participating in the combination. In appropriate cases, implement the default behavior for all classes that have a common interface. Declares an interface for accessing and managing component
leaf component (leaf) Role: represents a leaf node object in a group with no child nodes in the leaf node. Defines the behavior of an entity object in a group.
Branch Component (composite) Role: storage child part. Defines the behavior of those parts that have child parts. Implement actions related to child parts in the component interface.
Client: manipulate the objects of the assembly through the component interface
III, advantages and disadvantages of the compositing pattern
The benefits of the composite Mode
1, simplifying client code
2, making it easier to add new types of components
Disadvantages of the composite model: making your design more generic and adding components to it can also create problems that make it difficult to limit the components in a combination
iv. Application of the synthetic model scenario
1, you want to represent the part of the object-the overall hierarchy
2. You want users to ignore the difference between a grouped object and a single object, and the user will uniformly use all the objects in the composite structure.
v. Synthetic mode and other modes
Adorner mode:decorator mode is often used with composite mode. When decoration is used with compositing, they usually have a common parent class. Therefore, the decoration must support the component interface with Add,remove and getchild operations
enjoy meta mode: Flyweight mode lets you share components, but no longer reference their parent parts
iterator mode: Itertor can be used to traverse composite
Visitor Mode: Visitor will be localized in operations and behavior that should have been distributed in the composite and leaf categories.
Six, the safe type synthesis pattern
declare in the composite class all the methods used to manage the subclass object. Such a practice is safe. Because leaf-type objects simply do not have a way to manage subclasses, the program will fail at compile time if the client uses these methods for the leaf class object. Compile pass, there will be no run-time errors
This shortcoming is not transparent enough, because the leaf class and the combined class will have different interfaces.
Seven, the safe type of synthesis pattern structure diagram
Eight, safe synthesis mode PHP sample
<?php/** * Abstract Component Role * * Interface Component {/** * returns its own instance */Public function getcomposite ();
/** * Example Method */Public function operation ();
}/** * Branch Component Role * * Class Composite implements Component {private $_composites;
Public Function __construct () {$this->_composites = array ();
The Public Function Getcomposite () {return $this;
/** * Sample method, invoke the operation method of each child object/Public function operation () {echo ' composite operation BEGIN:<BR ';
foreach ($this->_composites as $composite) {$composite->operation ();
Echo ' composite operation end:<br/><br/> '; /** * Aggregation Management method adds a child object * @param Component $component child object/Public function Add (Component $component) {$this-
>_composites[] = $component; /** * Aggregation Management method deletes a child object * @param Component $component Child Object * @return Boolean deletion succeeded/Public function remove (Co Mponent $component) {foreach ($this->_composites as $key => $row) {if ($coMponent = = $row) {unset ($this->_composites[$key]);
return TRUE;
return FALSE;
The/** * Aggregation Management method returns all child objects/Public Function Getchild () {return $this->_composites;
} class Leaf implements Component {private $_name;
Public function __construct ($name) {$this->_name = $name;
The public Function operation () {echo ' Leaf operation ', $this->_name, ' <br/> ';
The Public Function Getcomposite () {return null;
}/** * Client/class Client {/** * Main program.
*/public static function main () {$leaf 1 = new Leaf (' a ');
$leaf 2 = new Leaf (' second ');
$composite = new Composite ();
$composite->add ($leaf 1);
$composite->add ($leaf 2);
$composite->operation ();
$composite->remove ($leaf 2);
$composite->operation (); } client::main ();?>
This is the code that uses PHP to implement the synthetic mode , and there are some conceptual distinctions about the synthetic model that I hope will help you learn.