PHP design pattern-bundle pattern Statement: This series of blog reference material "big talk design pattern", author Cheng Jie.
Combination mode (sometimes called partial-overall mode), which combines objects into a tree structure to represent a "partial whole" hierarchy. The combination mode ensures consistency between the use of a single object and a composite object. It makes the tree structure problem blur the concept of simple elements and complex elements. the customer program can process complex elements like simple elements, this decouples the customer program from the internal structure of complex elements.
Implementation class diagram:
Three roles:
Abstract structure (Company) role: This role implements the default behavior of all class common interfaces and declares an interface to manage sub-parts.
Leaf node (Technology) role: indicates a leaf object without a child node.
SubCompany role: used to store sub-parts and perform operations related to sub-parts, such as Add.
Implementation code:
Name = $ name;}/** added * @ param Company $ company subsidiary, department * @ return mixed */abstract function Add (Company $ company ); /** Remove * @ param Company $ company, department * @ return mixed */abstract function Remove (Company $ company ); /** Display the company and department structure * @ param $ depth * @ return mixed */abstract function Display ($ depth );} /** branch node subsidiary * Class Beijing */class SubCompany extends Company {private $ sub_companys = array (); function _ Construct ($ name) {parent ::__ construct ($ name);} function Add (Company $ company) {$ this-> sub_companys [] = $ company ;} 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 department * Class DeptCompany */class MoneyDept extends Company {function _ construct ($ name) {parent ::__ construct ($ name );} /** Add * @ param Company $ company, department * @ return mixed */function Add (Company $ company) {echo "leaf node, you cannot add more nodes ..........
";}/** Remove * @ param Company $ company subsidiary, department * @ return mixed */function Remove (Company $ company) {echo" leaf node, you cannot delete nodes ..........
";}/** Display 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 policydept extends Company {function _ construct ($ name) {parent :: __construct ($ name);}/** Add * @ param Company $ company subsidiary, department * @ return mixed */function Add (Company $ company) {echo "leaf node. you cannot add another node ..........
";}/** Remove * @ param Company $ company subsidiary, department * @ return mixed */function Remove (Company $ company) {echo" leaf node, you cannot delete nodes ..........
";}/** Display 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"); // ------------------------ code for testing the bundle mode ------------------ require_once ". /Composer. php "; $ root = new SubCompany (" Beijing Head Office "); $ root-> Add (new MoneyDept (" Head Office Finance ")); $ root-> Add (new TechnologyDept ("Head Office Technology Department"); $ shanghai = new SubCompany ("shanghai Branch "); $ shanghai-> Add (new TechnologyDept ("shanghai Branch Technology Department"); $ shanghai-> Add (new MoneyDept ("shanghai branch Finance Department ")); $ root-> Add ($ shanghai); $ root-> Display (1); echo"
"; $ Root-> Remove ($ shanghai); $ root-> Display (3 );
Advantages:
The combination mode allows you to optimize the processing of recursive or hierarchical data structures. There are many examples of hierarchical data structures, making the combination mode very useful. A general example of hierarchical data structure is what you encounter every time you use a computer: a file system. The file system consists of directories and files. Content can be installed in each directory. The contents of a directory can be files or directories. In this way, computer file systems are organized in recursive structures. If you want to describe such a data structure, you can use the Composite mode.
PHP object-oriented design model