Grouping objects into a tree-like structure to represent an entire hierarchy of parts, the composition mode allows users to treat individual objects and groups of objects uniformly.
The combination mode can generally be divided into three roles
- Abstract components: define the unified operations that need to be implemented
- Composite node: a composite object that can contain multiple objects, meaning that there may be other composite nodes and leaf nodes below it
- Leaf node: there will be no other nodes below (the only difference from the combination is that there are no child nodes)
The most commonly used combination pattern is to represent some tree-structured object relationships, such as the superior-subordinate relationship between departments or employees.
The following is an example of an employee subordinate relationship to implement a combination mode
Abstract component, which defines common properties and methods of some nodes
public abstract class AbstractNode {
private String name;
private AbstractNode parent;
public AbstractNode(String name) {
this.name = name;
}
public AbstractNode getParent() {
return parent;
}
public void setParent(AbstractNode parent) {
this.parent = parent;
}
public String getName(){
return name;
}
public abstract void addChild(AbstractNode node);
public abstract AbstractNode getChild(int i);
public abstract void removeChild(int i);
/**
* 员工自我介绍
*/
public abstract void introduce();
/**
* 团队统计
*/
public abstract int teamCount();
}
Combination nodes are equivalent to leadership, and some employees are managed under them
public class Node extends AbstractNode{
private List<AbstractNode> nodes;
public Node(String name) {
super(name);
nodes = new ArrayList<>();
}
@Override
public void addChild(AbstractNode node) {
node.setParent(this);
nodes.add(node);
}
@Override
public AbstractNode getChild(int i) {
return nodes.get(i);
}
@Override
public void removeChild(int i) {
nodes.remove(i);
}
@Override
public void introduce() {
String parentName = getParent()==null?" null":getParent().getName();
System.out.println("I am "+getName()+",my parent is "+parentName+
",I have "+nodes.size()+" child.");
for (AbstractNode node:nodes) {
node.introduce();
}
}
@Override
public int teamCount() {
int count = 0;
for (AbstractNode node:nodes) {
count += node.teamCount();
}
return count + 1;
}
}
Leaf nodes, equivalent to bottom employees, they are simpler to follow and combine nodes
public class Leaf extends AbstractNode{
public Leaf(String name) {
super(name);
}
@Override
public void addChild(AbstractNode node) {
throw new RuntimeException("这是叶子节点");
}
@Override
public AbstractNode getChild(int i) {
throw new RuntimeException("这是叶子节点");
}
@Override
public void removeChild(int i) {
throw new RuntimeException("这是叶子节点");
}
@Override
public void introduce() {
System.out.println("I am "+getName()+",my parent is "+getParent().getName()+".");
}
@Override
public int teamCount() {
return 1;
}
}
Client call
public class Client {
public static void main(String[] args) {
AbstractNode head = new Node("大领导");
AbstractNode midA = new Node("小领导A");
AbstractNode midB = new Node("小领导B");
AbstractNode staff1 = new Leaf("员工-1");
AbstractNode staff2 = new Leaf("员工-2");
AbstractNode staff3 = new Leaf("员工-3");
head.addChild(midA);
head.addChild(midB);
midA.addChild(staff1);
midA.addChild(staff2);
midB.addChild(staff3);
head.introduce();
System.out.println(head.getName()+"一共领导"+head.teamCount()+"人");
System.out.println(midA.getName()+"一共领导"+midA.teamCount()+"人");
}
}
==========结果==========
I am 大领导,my parent is null,I have 2 child.
I am 小领导A,my parent is 大领导,I have 2 child.
I am 员工-1,my parent is 小领导A.
I am 员工-2,my parent is 小领导A.
I am 小领导B,my parent is 大领导,I have 1 child.
I am 员工-3,my parent is 小领导B.
大领导一共领导6人
小领导A一共领导3人
The main purpose of the composition mode is to treat a group of objects as a whole through the composition relationship between the objects and operate them uniformly.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。