参考:http://c.biancheng.net/view/1373.html
什么是组合模式
组合(Composite Pattern)模式的定义:有时又叫做整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,用来表示“整体-部分”的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式。
组合模式一般用来描述整体与部分的关系,它将对象组织到树形结构中,顶层的节点称为根节点,根节点下面可以包含树枝节点和叶子节点,树枝节点下面又可以包含树枝节点和叶子节点,树形结构图如下:

由上图可以看出,其实根节点和树枝节点本质上属于同一种数据类型,可以作为容器使用;而叶子节点与树枝节点在语义上不属于用一种类型。但是在组合模式中,会把树枝节点和叶子节点看作属于同一种数据类型(用统一接口定义),让它们具备一致行为。
这样,在组合模式中,整个树形结构中的对象都属于同一种类型,带来的好处就是用户不需要辨别是树枝节点还是叶子节点,可以直接进行操作,给用户的使用带来极大的便利。
优点:
- 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,简化了客户端代码
- 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,符合“开闭原则”
缺点:
- 设计较复杂,客户端需要花更多时间理清类之间的层次关系
- 不容易限制容器中的构件
- 不容易用继承的方法来增加构件的新功能
模式的结构
组合模式包含以下主要角色。
- 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)
- 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。
- 树枝构件(Composite)角色 / 中间构件:是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
组合模式分为透明式的组合模式和安全式的组合模式。
1)透明方式
1 2 3 4 5 6 7
| public interface Component { public void operation(); public void add(Component c); public void remove(Component c); public Component getChild(int i); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| public class Leaf implements Component{ private String name;
public Leaf(String name) { this.name = name; } @Override public void add(Component c) { } @Override public void remove(Component c) { } @Override public Component getChild(int i) { return null; }
@Override public void operation() { System.out.println(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Composite implements Component{ private String name;
public Composite(String name) { this.name = name; }
ArrayList<Component> children = new ArrayList<>();
@Override public void add(Component c) { children.add(c); } @Override public void remove(Component c) { children.remove(c); }
@Override public Component getChild(int i) { return children.get(i); }
@Override public void operation() { System.out.println(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class Main { public static void main(String[] args) { Component root = new Composite("root"); Component c0 = new Composite("c0"); Component c1 = new Composite("c1"); Component c2 = new Composite("c2"); Component leaf0 = new Leaf("leaf0"); Component leaf1 = new Leaf("leaf1"); Component leaf2 = new Leaf("leaf2"); c0.add(c1); c0.add(leaf0); c1.add(leaf1); c2.add(leaf2); root.add(c0); root.add(c2);
tree(root,0); }
public static void tree(Component root,int depth){ for (int i=0;i<depth;i++){ System.out.print("--"); } root.operation(); if (root instanceof Composite){ for (Component c:((Composite)root).children){ tree(c,depth+1); } } } }
|
运行结果:
1 2 3 4 5 6 7
| root --c0 ----c1 ------leaf1 ----leaf0 --c2 ----leaf2
|
2)安全模式
1 2 3
| public interface Component { public void operation(); }
|
1 2 3 4 5 6 7 8 9 10 11 12
| public class Leaf implements Component{ private String name;
public Leaf(String name) { this.name = name; }
@Override public void operation() { System.out.println(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class Composite implements Component{ private String name;
public Composite(String name) { this.name = name; }
ArrayList<Component> children = new ArrayList<>();
public void add(Component c) { children.add(c); }
public void remove(Component c) { children.remove(c); }
public Component getChild(int i) { return children.get(i); }
@Override public void operation() { System.out.println(name); } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class Main { public static void main(String[] args) { Composite root = new Composite("root"); Composite c0 = new Composite("c0"); Composite c1 = new Composite("c1"); Composite c2 = new Composite("c2"); Component leaf0 = new Leaf("leaf0"); Component leaf1 = new Leaf("leaf1"); Component leaf2 = new Leaf("leaf2"); c0.add(c1); c0.add(leaf0); c1.add(leaf1); c2.add(leaf2); root.add(c0); root.add(c2);
tree(root,0); }
public static void tree(Component root,int depth){ for (int i=0;i<depth;i++){ System.out.print("--"); } root.operation(); if (root instanceof Composite){ for (Component c:((Composite)root).children){ tree(c,depth+1); } } } }
|
运行结果同上一样。
应用实例
【例1】用组合模式实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。
说明:假如李先生到韶关“天街e角”生活用品店购物,用 1 个红色小袋子装了 2 包婺源特产(单价 7.9 元)、1 张婺源地图(单价 9.9 元);用 1 个白色小袋子装了 2 包韶关香藉(单价 68 元)和 3 包韶关红茶(单价 180 元);用 1 个中袋子装了前面的红色小袋子和 1 个景德镇瓷器(单价 380 元);用 1 个大袋子装了前面的中袋子、白色小袋子和 1 双李宁牌运动鞋(单价 198 元)。
1 2 3 4 5 6 7 8
| public interface Articles { public void show();
public float calculation(); }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public class Goods implements Articles{ private String name; private int quantity; private float unitPrice;
public Goods(String name, int quantity, float unitPrice) { this.name = name; this.quantity = quantity; this.unitPrice = unitPrice; }
@Override public void show() { System.out.println(name + "(数量:" + quantity + ",单价:" + unitPrice + "元)"); }
@Override public float calculation() { return quantity*unitPrice; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public class Bags implements Articles{ private String name; ArrayList<Articles> bags = new ArrayList<>();
public Bags(String name) { this.name = name; }
public void add(Articles articles){ bags.add(articles); }
@Override public void show() { System.out.println("这是个"+name+"里面有,总价为:"+calculation()); }
@Override public float calculation() { float s = 0; for(Object o:bags){ s+=((Articles)o).calculation(); } return s; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| public class Main { public static void main(String[] args) { Bags smallRadBag = new Bags("红色小袋子"); Bags smallWhiteBag = new Bags("白色小袋子"); Bags bigBag = new Bags("大袋子"); Bags mediumBag = new Bags("中袋子");
Articles sp1 = new Goods("婺源特产",2,7.9f); Articles sp2 = new Goods("婺源地图",1,9.9f); Articles sp3 = new Goods("韶关香藉",2,68f); Articles sp4 = new Goods("韶关红茶",3,180f); Articles sp5 = new Goods("景德镇瓷器",1,380f); Articles sp6 = new Goods("李宁牌运动鞋",1,198f);
smallRadBag.add(sp1); smallRadBag.add(sp2);
smallWhiteBag.add(sp3); smallWhiteBag.add(sp4);
mediumBag.add(smallRadBag); mediumBag.add(sp5);
bigBag.add(mediumBag); bigBag.add(smallWhiteBag); bigBag.add(sp6);
tree(bigBag,0); }
public static void tree(Articles root,int depth){ for (int i = 0; i < depth; i++) { System.out.print("--"); } root.show(); if (root instanceof Bags){ for(Articles a:((Bags)root).bags){ tree(a,depth+1); } } } }
|
运行结果:
1 2 3 4 5 6 7 8 9 10
| 这是个大袋子里面有,总价为:1279.7 --这是个中袋子里面有,总价为:405.7 ----这是个红色小袋子里面有,总价为:25.7 ------婺源特产(数量:2,单价:7.9元) ------婺源地图(数量:1,单价:9.9元) ----景德镇瓷器(数量:1,单价:380.0元) --这是个白色小袋子里面有,总价为:676.0 ----韶关香藉(数量:2,单价:68.0元) ----韶关红茶(数量:3,单价:180.0元) --李宁牌运动鞋(数量:1,单价:198.0元)
|
应用场景
前面分析了组合模式的结构与特点,下面分析它适用的以下应用场景。
- 在需要表示一个对象整体与部分的层次结构的场合。
- 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。
扩展
如果对前面介绍的组合模式中的树叶节点和树枝节点进行抽象,也就是说树叶节点和树枝节点还有子节点,这时组合模式就扩展成复杂的组合模式了,如 Java AWT/Swing 中的简单组件 JTextComponent 有子类 JTextField、JTextArea,容器组件 Container 也有子类 Window、Panel。复杂的组合模式的结构图如图所示。
