Preface
==============================================================================
This PHP design mode album from the blog (jymoz.com), now has no access, this series of articles I have been looking for a long time to find the complete, thanks to the author Jymoz's Hard Pay Oh!
This address:http://www.cnblogs.com/davidhhuan/p/4248201.html
==============================================================================
Inside the star we can download maps made by others, or make maps for ourselves to play.
When we choose which map to play, we can see that the game lists the name of the map or map package in the current map package.
Although maps and map packages are differentiated by file and folder, we always want to use objects for abstraction when we develop them.
So can we simplify the difference between the two related objects of maps and map packages?
problem to be solved: try to match the code of the two objects as much as possible, that is, there are many occasions where you don't have to distinguish between a map and a map package.
idea: We do an abstract class that lets the map class and the map package class inherit it, so that the names of many methods of the class are the same.
Combination (Composite) mode example:
<?PHP//Abstract Map Classes Abstract classAbstractmap {//the name of the map or map package Public $name; //Construction Method Public function__construct ($name) { $this->name =$name; } //the name of the map or map package, the Map object has no sub-objects, so use empty functions, directly inherit Public functionGetChildren () {}//Add child objects, map objects have no sub-objects, so use empty functions, directly inherit Public functionAddChild (Abstractmap$child){} //show the name of a map or map package Public functionShowmapname () {Echo $this->name. " <br>"; } //Display sub-objects, map objects have no sub-objects, so use empty functions, directly inherit Public functionShowchildren () {}}//map class, inherit abstract map, we use abstract map in this way classMapextendsAbstractmap {}//map Package class, inherit abstract map, in which we need to overload the method of abstract map classMapbagextendsAbstractmap {//collection of child objects Public $childern; //Add sub-objects, Force Abstractmap objects, and of course maps and map packages are abstractmap objects because they inherit abstractmap. Public functionAddChild (Abstractmap$child) { $this->childern[] =$child; } //adding child objects Public function functionShowchildren () {if(Count($this->childern) >0) { foreach($this->childern as $child) { //call the name of the map or package $child-Showmapname (); } } } } //Create a new map package object, assuming the folder name is allied, you can look at the Star map directory, the real existence of $map 1=Newmapbag (' Allied '); //Create a new map object, assuming the file name is (2) fire Walker (also true) $map 2=NewMap (' (2Fire Walker '); //then you can see the characteristics and usefulness of the combined pattern. Assuming that the following code needs to manipulate two objects, and we assume that the two objects are not clear who is the map, who is the map package//To $MAP1 Add a sub-object to it, is a map, (4) the Gardens $map 1->addchild (NewMap (' (4) ( Gardens ')); //show its sub-objects $map 1-Showchildren (); //Add a sub-object to $MAP2, it's a map, (2) fire Walker, there's no error, because the map inherits an empty Add method $map 2->addchild (NewMap (' (2Fire Walker ')); //It's not going to go wrong because the map inherits an empty presentation method . $map 2-Showchildren ();?>
Summary of Use: The combination mode can be used to handle containers and objects (map packages and maps here), while other code handles these objects without being overly responsible for who is the container and who is the object. In order to simplify the explanation, there is no further discussion, in fact, the combination pattern is often used in conjunction with the iterative pattern, for example, we can use a uniform method (like the Showchildren method here), get all the map names under the map package (including subdirectories)
Implementation Summary: use a base class to implement methods that are common to containers and objects, such as the abstractmap above, and then let the container and object classes inherit the base class. Because of their different characteristics, the corresponding methods, such as the Addchild method, are overloaded in containers and object classes. In this way, the two objects can be manipulated in a uniform way.
Related articles:
1. StarCraft PHP Object-oriented (i)
2. StarCraft PHP Object-oriented (ii)
3. StarCraft PHP design mode-Simple Factory mode
4. StarCraft PHP Design mode-factory method mode
5. StarCraft PHP design mode-Abstract Factory mode
6. StarCraft PHP design mode-builder mode
7. The PHP design mode of StarCraft--The mediator mode
8. StarCraft PHP design mode--Enjoy meta mode
9. StarCraft PHP Design mode--proxy mode
10. StarCraft PHP design mode-prototype mode
11. The PHP design mode of StarCraft--Memo mode
12. StarCraft PHP design mode-template mode
13. StarCraft PHP design mode-positive mode
14. StarCraft PHP design mode-state mode
15. StarCraft PHP design mode--strategy mode
16. StarCraft PHP Design mode--combination mode
17. StarCraft PHP Design mode--responsibility chain mode
18. StarCraft PHP design mode-Observer mode
19. The PHP design mode of StarCraft--iterator mode
? 20. StarCraft PHP design mode-Adapter mode
16. StarCraft PHP Design mode--combination mode