Tree-like composite pattern

巴扎黑
Release: 2016-11-12 14:08:45
Original
1484 people have browsed it

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:

Tree-like composite pattern

The code is as follows:

<?php
abstract class Corp {
private $name = &#39;&#39;;
private $position = &#39;&#39;;
private $salary = 0;
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;
}
}
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(&#39;马总&#39;,&#39;总经理&#39;,100000);
$branch1 = new Branch(&#39;罗总&#39;,&#39;研发部门经理&#39;,20000);
$branch2 = new Branch(&#39;高总&#39;,&#39;销售部门经理&#39;,80000);
$leaf1 = new Leaf(&#39;张三&#39;,&#39;开发人员&#39;,7000);
$leaf2 = new Leaf(&#39;李四&#39;,&#39;开发人员&#39;,8000);
$leaf3 = new Leaf(&#39;二蛋&#39;,&#39;销售人员&#39;,10000);
$leaf4 = new Leaf(&#39;狗子&#39;,&#39;销售人员&#39;,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);
?>
Copy after login

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?

Tree-like composite pattern

It’s actually very simple. Just set its parent node when adding a node. The code is as follows:

<?php
abstract class Corp {
private $name = &#39;&#39;;
private $position = &#39;&#39;;
private $salary = 0;
<strong>private $parent = null;</strong>
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;
}
<strong>public function setParent($parent){
$this->parent = $parent;
}
public function getParent(){
return $this->parent;
}</strong>
}
class Leaf extends Corp{
}
class Branch extends Corp{
private $subordinateList = array();
public function addSubordinate(Corp $corp){
<strong>$corp->setParent($this);</strong>
array_push($this->subordinateList, $corp);
}
public function getSubordinateList(){
return $this->subordinateList;
}
}
$root = new Branch(&#39;马总&#39;,&#39;总经理&#39;,100000);
$branch1 = new Branch(&#39;罗总&#39;,&#39;研发部门经理&#39;,20000);
$branch2 = new Branch(&#39;高总&#39;,&#39;销售部门经理&#39;,80000);
$leaf1 = new Leaf(&#39;张三&#39;,&#39;开发人员&#39;,7000);
$leaf2 = new Leaf(&#39;李四&#39;,&#39;开发人员&#39;,8000);
$leaf3 = new Leaf(&#39;二蛋&#39;,&#39;销售人员&#39;,10000);
$leaf4 = new Leaf(&#39;狗子&#39;,&#39;销售人员&#39;,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);
?>
Copy after login

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.

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!