PHP design mode-compositing mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.