1. 定义
组合模式(Composite),将对象组合成树形结构以表示‘部分-整体’的层次结构。组合模式使得用户对单个对象和组合对象的使用具有一致性。
2. 组合模式的类图
3. 组合模式的基本代码
component为组合中的对象声明接口,在适当情况下实现所有类共有接口的默认行为。声明一个接口用于访问和管理component的子部件。
public abstract class Component {
private String name;
public Component(String name) {
this.name = name;
}
public abstract void add(Component c);
public abstract void remove(Component c);
public abstract void display(int depth);
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Leaf在组合中表示叶节点对象,叶节点没有子节点。
public class Leaf extends Component {
public Leaf(String name) {
super(name);
}
@Override
public void add(Component c) {
System.out.println("叶节点没有子节点,故不能添加");
}
@Override
public void remove(Component c) {
System.out.println("叶节点没有子节点,故不能删除");
}
@Override
public void display(int depth) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb)+this.getName());
}
}
Composite定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关的操作,比如增加add和删除remove。
public class Composite extends Component {
private List<Component> children = new ArrayList<Component>();
public Composite(String name) {
super(name);
}
@Override
public void add(Component c) {
children.add(c);
}
@Override
public void remove(Component c) {
children.remove(c);
}
@Override
public void display(int depth) {
StringBuffer sb = new StringBuffer();
for (int i = 0; i < depth; i++) {
sb.append("-");
}
System.out.println(new String(sb)+this.getName());
for (Component c : children) {
c.display(depth+2);
}
}
}
客户端代码,能通过Component接口操作组合部件的对象。
public static void main(String[] args) {
//生成树根,根上长出两叶Leaf A和Leaf B
Composite root = new Composite("root");
root.add(new Leaf("Leaf A"));
root.add(new Leaf("Leaf B"));
//根上长出分支Composite X,分支上也有两叶Leaf XA和Leaf XB
Composite comp = new Composite("Composite X");
comp.add(new Leaf("Leaf XA"));
comp.add(new Leaf("Leaf XB"));
root.add(comp);
//在分支Composite X上长出分支Composite XY,分支上也有两叶Leaf XYA和Leaf XYB
Composite comp2 = new Composite("Composite XY");
comp2.add(new Leaf("Leaf XYA"));
comp2.add(new Leaf("Leaf XYB"));
comp.add(comp2);
//根部又长出两叶Leaf C和Leaf D,Leaf D又被移除了
root.add(new Leaf("Leaf C"));
Leaf leaf = new Leaf("Leaf D");
root.add(leaf);
root.remove(leaf);
root.display(0);
}
执行结果:
root
--Leaf A
--Leaf B
--Composite X
----Leaf XA
----Leaf XB
----Composite XY
------Leaf XYA
------Leaf XYB
--Leaf C
4. 何时使用组合模式
当需求中是体现部分与整体层次的结构时,以及你希望用户可以忽略组合对象与单个对象的不同,统一地使用组合结构中的所有对象时,就应该考虑用组合模式了。