在现实生活中,存在很多“部分-整体”的关系,例如,大学中的部门与学院、总公司中的部门与分公司、学习用品中的书与书包、生活用品中的衣月艮与衣柜以及厨房中的锅碗瓢盆等。在软件开发中也是这样,例如,文件系统中的文件与文件夹、窗体程序中的简单控件与容器控件等。对这些简单对象与复合对象的处理,如果用组合模式来实现会很方便。
组合模式的定义与特点
组合(Composite)模式的定义:有时又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。
组合模式的主要优点有:
- 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
- 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;
其主要缺点是:
- 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
- 不容易限制容器中的构件;
- 不容易用继承的方法来增加构件的新功能;
组合模式的结构与实现
组合模式的结构不是很复杂,下面对它的结构和实现进行分析。
1. 模式的结构
组合模式包含以下主要角色。
- 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。
- 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中 声明的公共接口。
- 树枝构件(Composite)角色:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。
组合模式分为透明式的组合模式和安全式的组合模式。
(1) 透明方式:在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。其结构图如图 1 所示。
图1 透明式的组合模式的结构图
(2) 安全方式:在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。其结构图如图 2 所示。
图2 安全式的组合模式的结构图
2. 模式的实现
假如要访问集合 c0={leaf1,{leaf2,leaf3}} 中的元素,其对应的树状图如图 3 所示。
图3 集合c0的树状图
1 package composite; 2 import java.util.ArrayList; 3 public class CompositePattern 4 { 5 public static void main(String[] args) 6 { 7 Component c0=new Composite(); 8 Component c1=new Composite(); 9 Component leaf1=new Leaf("1"); 10 Component leaf2=new Leaf("2"); 11 Component leaf3=new Leaf("3"); 12 c0.add(leaf1); 13 c0.add(c1); 14 c1.add(leaf2); 15 c1.add(leaf3); 16 c0.operation(); 17 } 18 } 19 //抽象构件 20 interface Component 21 { 22 public void add(Component c); 23 public void remove(Component c); 24 public Component getChild(int i); 25 public void operation(); 26 } 27 //树叶构件 28 class Leaf implements Component 29 { 30 private String name; 31 public Leaf(String name) 32 { 33 this.name=name; 34 } 35 public void add(Component c){ } 36 public void remove(Component c){ } 37 public Component getChild(int i) 38 { 39 return null; 40 } 41 public void operation() 42 { 43 System.out.println("树叶"+name+":被访问!"); 44 } 45 } 46 //树枝构件 47 class Composite implements Component 48 { 49 private ArrayList<Component> children=new ArrayList<Component>(); 50 public void add(Component c) 51 { 52 children.add(c); 53 } 54 public void remove(Component c) 55 { 56 children.remove(c); 57 } 58 public Component getChild(int i) 59 { 60 return children.get(i); 61 } 62 public void operation() 63 { 64 for(Object obj:children) 65 { 66 ((Component)obj).operation(); 67 } 68 } 69 }
组合模式的应用实例
【例1】用组合模式实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。
说明:假如李先生到韶关“天街e角”生活用品店购物,用 1 个红色小袋子装了 2 包婺源特产(单价 7.9 元)、1 张婺源地图(单价 9.9 元);用 1 个白色小袋子装了 2 包韶关香藉(单价 68 元)和 3 包韶关红茶(单价 180 元);用 1 个中袋子装了前面的红色小袋子和 1 个景德镇瓷器(单价 380 元);用 1 个大袋子装了前面的中袋子、白色小袋子和 1 双李宁牌运动鞋(单价 198 元)。
最后“大袋子”中的内容有:{1 双李宁牌运动鞋(单价 198 元)、白色小袋子{2 包韶关香菇(单价 68 元)、3 包韶关红茶(单价 180 元)}、中袋子{1 个景德镇瓷器(单价 380 元)、红色小袋子{2 包婺源特产(单价 7.9 元)、1 张婺源地图(单价 9.9 元)}}},现在要求编程显示李先生放在大袋子中的所有商品信息并计算要支付的总价。
本实例可按安全组合模式设计,其结构图如图 4 所示。
图4 韶关“天街e角”店购物的结构图
程序代码如下:
1 package composite; 2 import java.util.ArrayList; 3 public class ShoppingTest 4 { 5 public static void main(String[] args) 6 { 7 float s=0; 8 Bags BigBag,mediumBag,smallRedBag,smallWhiteBag; 9 Goods sp; 10 BigBag=new Bags("大袋子"); 11 mediumBag=new Bags("中袋子"); 12 smallRedBag=new Bags("红色小袋子"); 13 smallWhiteBag=new Bags("白色小袋子"); 14 sp=new Goods("婺源特产",2,7.9f); 15 smallRedBag.add(sp); 16 sp=new Goods("婺源地图",1,9.9f); 17 smallRedBag.add(sp); 18 sp=new Goods("韶关香菇",2,68); 19 smallWhiteBag.add(sp); 20 sp=new Goods("韶关红茶",3,180); 21 smallWhiteBag.add(sp); 22 sp=new Goods("景德镇瓷器",1,380); 23 mediumBag.add(sp); 24 mediumBag.add(smallRedBag); 25 sp=new Goods("李宁牌运动鞋",1,198); 26 BigBag.add(sp); 27 BigBag.add(smallWhiteBag); 28 BigBag.add(mediumBag); 29 System.out.println("您选购的商品有:"); 30 BigBag.show(); 31 s=BigBag.calculation(); 32 System.out.println("要支付的总价是:"+s+"元"); 33 } 34 } 35 //抽象构件:物品 36 interface Articles 37 { 38 public float calculation(); //计算 39 public void show(); 40 } 41 //树叶构件:商品 42 class Goods implements Articles 43 { 44 private String name; //名字 45 private int quantity; //数量 46 private float unitPrice; //单价 47 public Goods(String name,int quantity,float unitPrice) 48 { 49 this.name=name; 50 this.quantity=quantity; 51 this.unitPrice=unitPrice; 52 } 53 public float calculation() 54 { 55 return quantity*unitPrice; 56 } 57 public void show() 58 { 59 System.out.println(name+"(数量:"+quantity+",单价:"+unitPrice+"元)"); 60 } 61 } 62 //树枝构件:袋子 63 class Bags implements Articles 64 { 65 private String name; //名字 66 private ArrayList<Articles> bags=new ArrayList<Articles>(); 67 public Bags(String name) 68 { 69 this.name=name; 70 } 71 public void add(Articles c) 72 { 73 bags.add(c); 74 } 75 public void remove(Articles c) 76 { 77 bags.remove(c); 78 } 79 public Articles getChild(int i) 80 { 81 return bags.get(i); 82 } 83 public float calculation() 84 { 85 float s=0; 86 for(Object obj:bags) 87 { 88 s+=((Articles)obj).calculation(); 89 } 90 return s; 91 } 92 public void show() 93 { 94 for(Object obj:bags) 95 { 96 ((Articles)obj).show(); 97 } 98 } 99 }
图5 复杂的组合模式的结构图