A company is composed of members who work. Each member has different attributes (name, position, salary), forming a tree structure according to different levels. The general manager is the root node of this tree because he has no superiors. The department manager is the branch node because he has both superiors and subordinates. The minions are the leaf nodes because he is the lowest-level hard worker and has no subordinates. Now use the combination mode to display this tree. Class diagram:
The code is as follows:
name = $name; $this->position = $position; $this->salary = $salary; } public function getInfo() { $return = "姓名:".$this->name."\t"; $return .= "职位:".$this->position."\t"; $return .= "薪水:".$this->salary."\n"; return $return; } } class Leaf extends Corp{ } class Branch extends Corp{ private $subordinateList = array(); public function addSubordinate(Corp $corp){ array_push($this->subordinateList, $corp); } public function getSubordinateList(){ return $this->subordinateList; } } $root = new Branch('马总','总经理',100000); $branch1 = new Branch('罗总','研发部门经理',20000); $branch2 = new Branch('高总','销售部门经理',80000); $leaf1 = new Leaf('张三','开发人员',7000); $leaf2 = new Leaf('李四','开发人员',8000); $leaf3 = new Leaf('二蛋','销售人员',10000); $leaf4 = new Leaf('狗子','销售人员',15000); $root->addSubordinate($branch1); $branch1->addSubordinate($leaf1); $branch1->addSubordinate($leaf2); $root->addSubordinate($branch2); $branch2->addSubordinate($leaf3); $branch2->addSubordinate($leaf4); function getTreeInfo($branch){ echo $branch->getInfo(); $subordinateList = $branch->getSubordinateList(); foreach ($subordinateList as $value) { if($value instanceof Branch){ getTreeInfo($value); }else{ echo $value->getInfo(); } } } getTreeInfo($root); ?>
Running results:
Name: Mr. Ma Position: General Manager Salary: 100000
Name: Mr. Luo Position: R&D Department Manager Salary: 20000
Name: Zhang San Position: Developer Salary: 7000
Name: Li Si Position: Developer Salary: 8000
Name: Mr. Gao Position: Sales Department Manager Salary: 80000
Name: Erdan Position: Salesperson Salary: 10000
Name: Gouzi Position: Salesperson Salary: 15000
[Finished in 0.1s]
Definition of combination mode
combination mode is also called synthesis mode, also called part-whole mode, It is mainly used to describe the relationship between parts and the whole. It is defined as: combining objects into a tree structure to represent the "part-whole" hierarchical structure, so that users can use single objects and combined objects consistently. The combination mode is mainly composed of three roles
1. Component abstract component role
Define the common methods and properties of the combined objects. You can define some default behaviors or properties, such as the Corp class in the example.
2. Leaf component
3. Composite branch component
Combine branch nodes and leaf nodes to form a tree structure
Advantages of combination mode
1. Simple calling of high-level modules
In a tree-shaped structure All nodes are Components, and there is no difference between the part and the whole for the caller. That is to say, the high-level module does not have to care whether it is dealing with a single object or the entire combined structure, which simplifies the code of the high-level module.
2. The node can be added by itself
Just find its parent node. It is very easy to expand and complies with the opening and closing principle.
Disadvantages of the combination mode
The combination mode has a very obvious shortcoming. See the definition in the scene class, did you mention the definition when using leaves and branches? The implementation class is used directly! This is very inappropriate in interface-oriented programming, conflicts with the dependency delegation principle, and limits the scope of influence in the interface.
Usage scenarios of combination mode
1. Scenarios for maintaining and displaying part-whole relationships, such as tree menu, file, and folder management.
2. Scenarios where total price modules or functions can be independently separated from a whole.
Notes on the combination mode
As long as it is a tree structure, you must consider using the combination mode. This must be remembered, as long as it is to reflect the relationship between the part and the whole, and this relationship may be relatively deep. , you should consider the combination model.
Extension of combination mode
1. Real combination mode (skip)
2. Transparent combination mode
The example is a safe combination mode. The transparent mode puts the methods used for combination into abstract classes (Skip)
3. Combination mode traversal
In the example, the tree is traversed from the upper level to the lower level. If a leaf node is randomly selected, how to traverse to the upper level?
It’s actually very simple. Just set its parent node when adding a node. The code is as follows:
private $parent = null; public function __construct( $name, $position, $salary ) { $this->name = $name; $this->position = $position; $this->salary = $salary; } public function getInfo() { $return = "姓名:".$this->name."\t"; $return .= "职位:".$this->position."\t"; $return .= "薪水:".$this->salary."\n"; return $return; } public function setParent($parent){ $this->parent = $parent; } public function getParent(){ return $this->parent; } } class Leaf extends Corp{ } class Branch extends Corp{ private $subordinateList = array(); public function addSubordinate(Corp $corp){ $corp->setParent($this); array_push($this->subordinateList, $corp); } public function getSubordinateList(){ return $this->subordinateList; } } $root = new Branch('马总','总经理',100000); $branch1 = new Branch('罗总','研发部门经理',20000); $branch2 = new Branch('高总','销售部门经理',80000); $leaf1 = new Leaf('张三','开发人员',7000); $leaf2 = new Leaf('李四','开发人员',8000); $leaf3 = new Leaf('二蛋','销售人员',10000); $leaf4 = new Leaf('狗子','销售人员',15000); $root->addSubordinate($branch1); $branch1->addSubordinate($leaf1); $branch1->addSubordinate($leaf2); $root->addSubordinate($branch2); $branch2->addSubordinate($leaf3); $branch2->addSubordinate($leaf4); function getParentInfo($leaf){ echo $leaf->getInfo(); $parent = $leaf->getParent(); if($parent instanceof branch) getParentInfo($parent); } getParentInfo($leaf4); ?>
Run results:
Name: Gouzi Position: Salesperson Salary: 15000
Name: Mr. Gao Position: Sales Department Manager Salary: 80000
Name: Mr. Ma Position: General Manager Salary: 100000
[Finished in 0.2s]
The bold part in the code is the difference from the previous example.