This article mainly introduces the PHP combination mode detailed explanation and the case, under the interested friend’s reference, hoped to be helpful to everybody.
This model can be a little confusing to understand, especially in the difficult interpretations of some books. First of all, let’s talk about the characteristics of the combination mode:
1. Indivisible base elements must exist.
2. Combined objects can be combined.
To take a common example, an atom is the basic particle of a chemical reaction and is indivisible in it. There are four atoms, C, H, O, N, and they can be randomly combined into an infinite number of molecules, either proteins or fats, and proteins and fats are combinations. Protein and fat can be combined to make meat, soy, and so on.
Back to the topic, now there is a requirement that the customer creates a leaf, can set the size and color of the leaf, and can name the leaf.
abstract class tree{ abstract function create(); } class createLeaf extends tree{ private $name; private $size; private $color; private $leaf=array(); public function __construct($name,$size,$color){ $this->name=$name; $this->size=$size; $this->color=$color; } public function create(){ $this->leaf[$this->name]=array( 'size'=>$this->size, 'color'=>$this->color ); return $this->leaf; $leaf=new createLeaf(' red leaf ',' big ',' red '); print_r($leaf->create()); Array ([size] => Array ([size] => large [color] => red))
Our design perfectly fulfilled the customer’s needs, but now the customer has come up with a new requirement, not only to create leaves, but to create branches, and to be able to place the leaves on the branches, or to take the placed leaves off the branches. The end result, they hoped, would be a tree that could be planted with other branches
Analysis: Creating a leaf and creating a branch both have creation operations, so they can both implement abstract tree classes. But creating a branch class requires placement and removal, so let’s add two abstract methods combination() and separation() to the tree class for the moment.
abstract class tree{ abstract function create(); // Create abstract Function Combination (Tree $Item); // Combination of abstract function separation(Tree $item); // Split} Class createLeaf extends Tree {private $name; private $size; private $color; private $leaf=array(); public function __construct($name,$size,$color){ $this->name=$name; $this->size=$size; $this->color=$color; } public function create(){ $this->leaf[$this->name]=array( 'size'=>$this->size, 'color'=>$this->color ); return $this->leaf; } // Since there is no combination and separation required to create the leaf class, we throw the two methods an error warning. Public Function combination(tree $item){throw New Exception(" Combination operations are not supported in this class "); } Public function separation(tree $item){throw New Exception(" This class does not support separation "); } } class createBranch extends tree{ private $name; private $branch=array(); private $items=array(); // this variable is used to store the leaf object public function/construct($name){$this->name=$name; {$arr=$item->create();} // we already know that objects in $items contain create operations, so we can create public function create(){foreach($this->items as $item){$arr=$item->create(); $this->branch[$this->name][]=$arr; } if(empty($this->branch)){ $this->branch[$this->name]=array(); } return $this->branch; } public function combination(tree $item){ $this->items[]=$item; } public function separation(tree $item){ $key=array_search($item,$this->items); if($key! ==false){ unset($this->items[$key]); $leaf_1=new createLeaf(' red leaves ',' big ',' red '); $leaf_2=new createLeaf(' big green leaves ',' big green ',' green '); $leaf_3=new createLeaf(' rhubarb leaves ',' big ',' yellow '); $leaf_4=new createLeaf(' little red leaves ',' little red ',' red '); $leaf_5=new createLeaf(' little green leaves ',' little green ',' green '); $leaf_6=new createLeaf(' little yellow leaves ',' small ',' yellow '); $branch_1=new createBranch(' branch 1 '); $branch_1->combination($leaf_1); $branch_1->combination($leaf_2); $branch_1->combination($leaf_3); $branch_2=new createBranch(' branch 2 '); $branch_2->combination($leaf_4); $branch_2->combination($leaf_5); $branch_2->combination($leaf_6); $branch=new createBranch(' trunk '); $branch->combination($branch_1); $branch->combination($branch_2); print_r($branch->create()); Running the above code will result in: Array ([trunk] = > Array ([0] = > Array ([1] branches = > Array ([0] = > Array ([red] leaves = > Array ([size] = [color] = > > red)) [1] = > Array ([big green leaves] = > Array ([size] = > [color] = > green)] [2] = > Array ( [rhubarb leaves] = > Array ([size] = > [color] = > yellow)))) [1] = > Array ([2] branches = > Array ([0] = > Array ([small red leaves] = > Array ([size] = > small [color] = > red)) [1] = > Array ([little green leaves] = > Array ([size] = > small [color] = > green)) [2] = > Array ([yellow leaves] = > Array ([size] = > small [color] = > yellow))))))
We did this beautifully, creating a canopy tree, but here’s the problem. Creating leaves requires only a create() operation, no combination() and separation(). Why don’t we split the abstract tree into two classes?
abstract class tree{ abstract function create(); } // Because the abstract class of the split trunk is inherited from the tree, create() must be implemented, but the implementation of create() will cause code duplication, so this class is also declared as abstract class branch extends tree{abstract function combination(tree $item); abstract function separation(tree $item); } class createLeaf extends tree{ private $name; private $size; private $color; private $leaf=array(); public function __construct($name,$size,$color){ $this->name=$name; $this->size=$size; $this->color=$color; } public function create(){ $this->leaf[$this->name]=array( 'size'=>$this->size, 'color'=>$this->color ); return $this->leaf; Public function combination(tree $item){throw New Exception(" Combination operation is not supported in this class "); } Public function separation(tree $item){throw New Exception(" This class does not support separation "); } } class createBranch extends branch{ private $name; private $branch=array(); private $items=array(); public function __construct($name){ $this->name=$name; } public function create(){ foreach($this->items as $item){ $arr=$item->create(); $this->branch[$this->name][]=$arr; } if(empty($this->branch)){ $this->branch[$this->name]=array(); } return $this->branch; } public function combination(tree $item){ $this->items[]=$item; } public function separation(tree $item){ $key=array_search($item,$this->items); if($key! ==false){ unset($this->items[$key]); $leaf_1=new createLeaf(' red leaves ',' big ',' red '); $leaf_2=new createLeaf(' big green leaves ',' big green ',' green '); $leaf_3=new createLeaf(' rhubarb leaves ',' big ',' yellow '); $leaf_4=new createLeaf(' little red leaves ',' little red ',' red '); $leaf_5=new createLeaf(' little green leaves ',' little green ',' green '); $leaf_6=new createLeaf(' little yellow leaves ',' small ',' yellow '); $branch_1=new createBranch(' branch 1 '); $branch_1->combination($leaf_1); $branch_1->combination($leaf_2); $branch_1->combination($leaf_3); $branch_2=new createBranch(' branch 2 '); $branch_2->combination($leaf_4); $branch_2->combination($leaf_5); $branch_2->combination($leaf_6); $branch=new createBranch(' trunk '); $branch->combination($branch_1); $branch->combination($branch_2); print_r($branch->create());
In this way, we finally completed this requirement beautifully. It is important to note, however, that because of the flexibility of composition patterns, many people prefer to use composition classes without thinking. In fact, the composite class has the disadvantage of being too flexible and expensive. Let’s imagine that an element or combination may be called many times throughout the system, but once an element or combination has a problem in one node of the system, it will be very difficult to detect that node.
Imagine if some element of the system was an SQL statement querying the database, and the OVERHEAD of this SQL statement was a little too high, once it was combined into every corner of the system, the result of running the system would be disastrous.
Related recommendations:
PHP design pattern combination pattern
Combinational patterns and case sharing for PHP design patterns
PHP Visitor pattern and composition pattern in detail
The above is the PHP combination mode detailed explanation and the case detailed content, more please pay attention to IT learning garden other related articles!