PHP design mode combination mode, php Design Mode Combination
When an object may represent a single entity or a composite entity, but still needs to be used in the same way, this situation is suitable for the design of the composite mode.
The combination mode is a structural mode.
After reading the explanation in the book, I did not understand it very well. I went through the "big design model", and the following is the original article:
The explanation in this book is not very understandable, but it can be encoded. The following is the code of the combination mode:
<? Php/*** combination mode abstract base class */abstract class CompanyBase {// node name protected $ name; public function _ construct ($ name) {$ this-> name = $ name;} public function getName () {return $ this-> name;} // add node abstract function add (CompanyBase $ c ); // delete node abstract function remove (CompanyBase $ c); // output node information abstract function show ($ deep); // node responsibility abstract function work ($ deep );} /*** Company class */class Company extends CompanyBase {p Rotected $ item = []; public function add (CompanyBase $ c) {$ nodeName = $ c-> getName (); if (! Isset ($ this-> item [$ nodeName]) {$ this-> item [$ nodeName] = $ c;} else {throw new Exception ("this node already exists, node name :". $ nodeName) ;}} public function remove (CompanyBase $ c) {$ nodeName = $ c-> getName (); if (isset ($ this-> item [$ nodeName]) {unset ($ this-> item [$ nodeName]);} else {throw new Exception ("this node does not exist, node name :". $ nodeName) ;}} public function show ($ deep = 0) {echo str_repeat ("-", $ deep ). $ this-> name; echo "<br>"; foreach ($ this-> item as $ value) {$ value-> show ($ deep + 4 );}} public function work ($ deep = 0) {foreach ($ this-> item as $ value) {echo str_repeat ("& emsp;", $ deep ). "[{$ this-> name}] <br>"; $ value-> work ($ deep + 2 );}}} /*** Human Resources Department */class HumanResources extends CompanyBase {public function add (CompanyBase $ c) {throw new Exception ("nodes cannot be added under this node ");} public function remove (CompanyBase $ c) {throw new Exception ("No subnode under this node");} public function show ($ deep = 0) {echo str_repeat ("-", $ deep ). $ this-> name; echo "<br>";} public function work ($ deep = 0) {echo str_repeat ("& emsp;", $ deep ). "the job of the human resources department is to recruit talents for the company"; echo "<br> ";}} /*** Business Department */class Commerce extends CompanyBase {public function add (CompanyBase $ c) {throw new Exception ("nodes cannot be added under this node ");} public function remove (CompanyBase $ c) {throw new Exception ("No subnode under this node");} public function show ($ deep = 0) {echo str_repeat ("-", $ deep ). $ this-> name; echo "<br>";} public function work ($ deep = 0) {echo str_repeat ("& emsp;", $ deep ). "The business department's job is to make profits for the company"; echo "<br> ";}}
After the class is designed, you can use it:
$ C = new Company ("a technology Company in Beijing"); $ h = new HumanResources ("HR Department"); $ com = new Commerce ("Commerce Department "); $ c-> add ($ h); $ c-> add ($ com); // Tianjin Branch // to be lazy, the Department of the branch directly copies $ c1 = new Company ("Tianjin branch") of the parent Company; $ c1-> add ($ h); $ c1-> add ($ com ); $ c-> add ($ c1); // Wuhan branch $ c2 = new Company ("Wuhan branch"); $ c2-> add ($ h ); $ c2-> add ($ com); $ c-> add ($ c2); // use the company functions $ c-> show (); $ c-> work ();
Output:
Summarize the features of the following combination modes: