The combination mode (Composite pattern) is sometimes called the partial-whole pattern, which is used to group objects into a tree structure to represent a "partial-whole" hierarchical relationship. The combined mode makes the user consistent with the use of individual objects and composite objects.
Common usage scenarios: such as tree menu, folder menu, departmental organization chart, etc.
<?php/** * * Safe synthesis mode */interface Component {public Function getcomposite ();//return own instance public function operation ( );} Class Composite implements Component {//Branch component role private $_composites; Public Function __construct () {$this->_composites = array ();} Public Function Getcomposite () {return $this;} Public function operation () {foreach ($this->_composites as $composite) {$composite->operation ( ); }} Public Function Add (Component $component) {///Aggregation management method adds a sub-object $this->_composites[] = $component; The Public function remove (Component $component) {////Aggregation management method deletes a sub-object foreach ($this->_composites as $key + = $row) {if ($component = = $row) {unset ($this->_composites[$key]); return TRUE;} } return FALSE; } public Function Getchild () {////Aggregation management method returns all child objects return $this->_composites; }}class Leaf implements Component {private $_name; Public Function __cOnstruct ($name) {$this->_name = $name;} Public function operation () {} public Function Getcomposite () {return null;}} CLIENT$LEAF1 = New Leaf (' first '), $leaf 2 = new Leaf (' second '), $composite = new Composite (), $composite->add ($leaf 1); $composite->add ($leaf 2); $composite->operation (); $composite->remove ($leaf 2); $composite->operation () ;/** * * Transparent synthesis mode */interface Component {//Abstract component Role Public function getcomposite ();//return own instance public function Operat Ion (); Example method Public function add (Component $component); Aggregation Management method, add a child object public function remove (Component $component); The aggregation management method deletes a child object public function Getchild (); The aggregation management method returns all child objects}class Composite implements Component {//Branch component role private $_composites; Public Function __construct () {$this->_composites = array ();} Public Function Getcomposite () {return $this;} Public function operation () {///example method, calling operation method of each sub-object foreach ($this->_composites as $composite) {$composite->operation (); }} Public Function Add (Component $component) {///Aggregation management method adds a sub-object $this->_composites[] = $component; The Public function remove (Component $component) {////Aggregation management method deletes a sub-object foreach ($this->_composites as $key + = $row) {if ($component = = $row) {unset ($this->_composites[$key]); return TRUE;} } return FALSE; } public Function Getchild () {////Aggregation management method returns all child objects return $this->_composites; }}class Leaf implements Component {private $_name; Public function __construct ($name) {$this->_name = $name;} Public function operation () {echo $this->_name. <br> ";} Public Function Getcomposite () {return null;} Public function Add (Component $component) {return FALSE;} Public function Remove (Component $component) {return FALSE;} Public Function Getchild () {return null;}} Client $leaf 1 = new Leaf (' first '), $leaf 2 = new Leaf (' second '); $composite = new CompOsite (); $composite->add ($leaf 1); $composite->add ($leaf 2); $composite->operation (); $composite->remove ($leaf 2); $composite->operation ();? >
PHP design mode-compositing mode