Statement: This series of blog reference "Big Talk design mode", author Geoscience.
Combined mode (sometimes called partial-overall mode), combines objects into a tree structure to represent a hierarchical structure of "partial whole". The combined mode makes the user consistent with the use of individual objects and composite objects. It blurs the concept of simple elements and complex elements in the problem of our tree structure, and the client program can handle complex elements like simple elements, thus decoupling the client program from the internal structure of the complex elements.
To implement a class diagram:
Three major roles:
Abstract structure (company) role: This role implements the default behavior for all classes of common interfaces, declaring an interface management subassembly.
Leaf node (technology) Role: Represents a leaf object with no child nodes.
Side Point (subcompany) role: Used to store sub-parts to implement operations related to subassemblies, such as Add ().
Implementation code:
Name= $name; /** added * @param company $company subsidiaries, Department * @return Mixed */abstract function Add (companies $company); /** Remove * @param company $company subsidiaries, Department * @return Mixed */abstract function Remove (companies $company); /** Display Company and departmental structure * @param $depth * @return Mixed */abstract function display ($depth);} /** Minor Pips Company * Class Beijing */class Subcompany extends company{private $sub _companys=array (); function __construct ($name) {parent::__construct ($name); The function Add (company $company) {$this->sub_companys[]= $company; The function Remove (company $company) {$key =array_search ($company, $this->sub_companys); if ($key!==false) {unset ($this->sub_companys[$key]); }} function Display ($depth) {$pre = ""; for ($i =0; $i < $depth; $i + +) {$pre. = "-"; } $pre. = $this->name. "
"; Echo $pre; foreach ($this->sub_companys as $v) {$v->display ($depth +2); }}}/** leaf Node Finance Dept. * Class Deptcompany */class moneydept extends company{function __construct ($name ) {parent::__construct ($name); }/** added * @param company $company subsidiaries, Department * @return Mixed */function Add (companies $company) {E Cho "leaf node, cannot continue adding nodes ....
"; /** Remove * @param company $company subsidiaries, Department * @return Mixed */function remove (companies $company) { echo "leaf node, cannot delete node .....
"; }/** displays the company and department structure * @param $depth * @return Mixed */function display ($depth) {$pre = ""; for ($i =0; $i < $depth; $i + +) {$pre. = "-"; } $pre. = $this->name. "
"; Echo $pre; }}/** Leaf Node Technical Department * Class Deptcompany */class technologydept extends company{function __construct ($nam e) {parent::__construct ($name); }/** added * @param company $company subsidiaries, Department * @return Mixed */function Add (companies $company) {E Cho "leaf node, cannot continue adding nodes ....
"; /** Remove * @param company $company subsidiaries, Department * @return Mixed */function remove (companies $company) { echo "leaf node, cannot delete node .....
"; }/** displays the company and department structure * @param $depth * @return Mixed */function display ($depth) {$pre = ""; for ($i =0; $i < $depth; $i + +) {$pre. = "-"; } $pre. = $this->name. "
"; Echo $pre; }}
Test code:
Header ("Content-type:text/html;charset=utf-8");//------------------------the combo mode test code------------------require_ Once "./composer/composer.php"; $root =new Subcompany ("Beijing head Office"); $root->add (New Moneydept ("head Office Finance Department"); $root->add (New Technologydept ("Head Office Technology Department"), $shanghai =new subcompany ("Shanghai Branch"), $shanghai->add (New technologydept ("Shanghai Branch Technical Department") $shanghai->add (New moneydept ("Shanghai Branch Finance Department"), $root->add ($shanghai); $root->display (1); echo "
"; $root->remove ($shanghai); $root->display (3);
Advantage:
The combined mode allows you to optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures that make combinatorial patterns very useful. A universal example of hierarchical data structures is what you encounter every time you use a computer: the file system. The file system consists of directories and files. Each directory can be loaded with content. The contents of a directory can be either a file or a directory. In this way, the computer's file system is organized in a recursive structure. If you want to describe such a data structure, then you can use the combined mode composite.
PHP Object-oriented design pattern