组合模式

在显示生活中,存在很多“部分-整体”的关系,例如,大学中的部门与学院、总公司中的部门与分公司、学习用品中的书与书包等。在软件开发中也是这样,例如,文件系统中的文件与文件夹、窗体程序中的简单控制件与容器控件等。对这些简单对象与符合对象的处理,如果用组合模式来实现会很方便。

一、组合模式的定义与特点

组合模式的定义:有时又叫做整体-部分模式,它是一种将对象组合成树状的层次结构的模式,用来表示“”整体-部分“的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式。

组合模式一般用来描述整体与部分的关系,它将对象组织到树形结构中,顶层的节点被称为根节点,根节点下面可以包含树枝节点和叶子结点,树枝节点下边又可以包含树脂及诶单和叶子结点,树形结构图如下:

由上图可以看出,其实根节点和树枝节点本质上属于同一种数据类型,可以作为容器使用;而叶子结点与树枝节点在语义上不属于同一种类型。但是在组合模式中,会把树枝节点和叶子结点看做属于同一种数据烈性,让他们具备一致行为。

这样,在组合模式中,整个树形结构中的对象都属于同一种类型,带来的好处就是用户不需要辨别是树枝节点还是叶子结点,可以直接进行操作,给用户带来了极大的便利。

优点

  1. 组合模式使得客户端代码可以一致的处理单个对象和组合对象,无须关心自己处理的是单个对象还是组合对象,简化了客户端的代码
  2. 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;

缺点

  1. 设计比较复杂,客户端需要花更多的时间来理清类之间的层次关系
  2. 不容易限制容器中的构件
  3. 不容易用继承的方法来增加构件的新功能

二、组合模式的结构与实现

1. 模式的结构

组合模式包含以下主要角色:

  1. 构象构件角色(Component):它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)
  2. 树叶构件角色(Leaf):是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。
  3. 树枝构件角色(Composite):是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

组合模式分为透明式的组合模式安全式的组合模式

(1)透明方式

在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。其结构图如图所示

(2)安全方式

在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。其结构图如图所示。

2.模式的实现

透明模式

  1. /**
  2. * @author liyuan
  3. * @date 2021年06月24日 17:30
  4. * 抽象构件
  5. */
  6. public interface Component {
  7. public void add(Component c);
  8. public void remove(Component c);
  9. public Component getChild(int i);
  10. public void operation();
  11. }
  12. /**
  13. * @author liyuan
  14. * @date 2021年06月24日 17:31
  15. * 树枝构件
  16. */
  17. public class Composite implements Component{
  18. private ArrayList<Component> children = new ArrayList<Component>();
  19. @Override
  20. public void add(Component c) {
  21. children.add(c);
  22. }
  23. @Override
  24. public void remove(Component c) {
  25. children.remove(c);
  26. }
  27. @Override
  28. public Component getChild(int i) {
  29. return children.get(i);
  30. }
  31. @Override
  32. public void operation() {
  33. for (Object obj : children) {
  34. ((Component) obj).operation();
  35. }
  36. }
  37. }
  38. /**
  39. * @author liyuan
  40. * @date 2021年06月24日 17:31
  41. * 树叶构件
  42. */
  43. public class Leaf implements Component{
  44. private String name;
  45. public Leaf(String name) {
  46. this.name = name;
  47. }
  48. @Override
  49. public void add(Component c) {
  50. }
  51. @Override
  52. public void remove(Component c) {
  53. }
  54. @Override
  55. public Component getChild(int i) {
  56. return null;
  57. }
  58. @Override
  59. public void operation() {
  60. System.out.println("树叶" + name + ":被访问!");
  61. }
  62. }
  63. /**
  64. * @author liyuan
  65. * @date 2021年06月24日 17:32
  66. * 透明模式
  67. */
  68. public class Test {
  69. public static void main(String[] args) {
  70. Component c0 = new Composite();
  71. Component c1 = new Composite();
  72. Component leaf1 = new Leaf("1");
  73. Component leaf2 = new Leaf("2");
  74. Component leaf3 = new Leaf("3");
  75. c0.add(leaf1);
  76. c0.add(c1);
  77. c1.add(leaf2);
  78. c1.add(leaf3);
  79. c0.operation();
  80. }
  81. }

安全模式

与透明模式不一样的点在于

  1. Component只保留层次的公共行为。
  2. 修改客户端代码,将树枝构建类型更改为Composite类型,以便获取管理子类的操作方法。
  1. /**
  2. * @author liyuan
  3. * @date 2021年06月24日 17:37
  4. */
  5. public interface Component {
  6. // 改造点
  7. public void operation();
  8. }
  9. /**
  10. * @author liyuan
  11. * @date 2021年06月24日 17:31
  12. * 树枝构件
  13. */
  14. public class Composite implements Component {
  15. private ArrayList<Component> children = new ArrayList<Component>();
  16. public void add(Component c) {
  17. children.add(c);
  18. }
  19. public void remove(Component c) {
  20. children.remove(c);
  21. }
  22. public Component getChild(int i) {
  23. return children.get(i);
  24. }
  25. @Override
  26. public void operation() {
  27. for (Object obj : children) {
  28. ((Component) obj).operation();
  29. }
  30. }
  31. }
  32. /**
  33. * @author liyuan
  34. * @date 2021年06月24日 17:31
  35. * 树叶构件
  36. */
  37. public class Leaf implements Component {
  38. private String name;
  39. public Leaf(String name) {
  40. this.name = name;
  41. }
  42. @Override
  43. public void operation() {
  44. System.out.println("树叶" + name + ":被访问!");
  45. }
  46. }
  47. /**
  48. * @author liyuan
  49. * @date 2021年06月24日 17:37
  50. * 安全模式
  51. */
  52. public class Test {
  53. public static void main(String[] args) {
  54. // 改造点
  55. Composite c0 = new Composite();
  56. Composite c1 = new Composite();
  57. Component leaf1 = new Leaf("1");
  58. Component leaf2 = new Leaf("2");
  59. Component leaf3 = new Leaf("3");
  60. c0.add(leaf1);
  61. c0.add(c1);
  62. c1.add(leaf2);
  63. c1.add(leaf3);
  64. c0.operation();
  65. }
  66. }

三、组合模式的应用实例

【例】用组合模式实现当用户在商店购物后,显示其所选商品信息,并计算所选商品总价的功能。

说明:加入李先生到生活用品商店购物,用1个红色小袋子装了2包特产(单价7.9元)、1张地图(单价9.9元);用一个白色小袋子装了2瓶白酒(单价68元)和3包红茶(单价180元);用一个中袋子装了前面的红色小袋子和一个锅(单价380元);用一个大袋子装了前面的中袋子、白色小袋子和一双运动鞋(单价198元),显示所有商品信息并且计算总价。

代码如下

  1. /**
  2. * @author liyuan
  3. * @date 2021年06月25日 10:33
  4. * 抽象构建:物品
  5. */
  6. public interface Articles {
  7. // 计算
  8. public float calculation();
  9. public void show();
  10. }
  11. /**
  12. * @author liyuan
  13. * @date 2021年06月25日 10:34
  14. * 树叶构件:商品
  15. */
  16. public class Goods implements Articles{
  17. private String name; //名字
  18. private int quantity; //数量
  19. private float unitPrice; //单价
  20. public Goods(String name, int quantity, float unitPrice) {
  21. this.name = name;
  22. this.quantity = quantity;
  23. this.unitPrice = unitPrice;
  24. }
  25. @Override
  26. public float calculation() {
  27. return quantity * unitPrice;
  28. }
  29. @Override
  30. public void show() {
  31. System.out.println(name + "(数量:" + quantity + ",单价:" + unitPrice + "元)");
  32. }
  33. }/**
  34. * @author liyuan
  35. * @date 2021年06月25日 10:35
  36. * 树枝构件:袋子
  37. */
  38. public class Bags implements Articles {
  39. private String name; //名字
  40. private ArrayList<Articles> bags = new ArrayList<Articles>();
  41. public Bags(String name) {
  42. this.name = name;
  43. }
  44. public void add(Articles c) {
  45. bags.add(c);
  46. }
  47. public void remove(Articles c) {
  48. bags.remove(c);
  49. }
  50. public Articles getChild(int i) {
  51. return bags.get(i);
  52. }
  53. @Override
  54. public float calculation() {
  55. float s = 0;
  56. for (Object obj : bags) {
  57. s += ((Articles) obj).calculation();
  58. }
  59. return s;
  60. }
  61. @Override
  62. public void show() {
  63. for (Object obj : bags) {
  64. ((Articles) obj).show();
  65. }
  66. }
  67. }
  68. /**
  69. * @author liyuan
  70. * @date 2021年06月25日 10:38
  71. */
  72. public class Test {
  73. public static void main(String[] args) {
  74. float s = 0;
  75. Bags BigBag, mediumBag, smallRedBag, smallWhiteBag;
  76. Goods sp;
  77. BigBag = new Bags("大袋子");
  78. mediumBag = new Bags("中袋子");
  79. smallRedBag = new Bags("红色小袋子");
  80. smallWhiteBag = new Bags("白色小袋子");
  81. sp = new Goods("特产",2,7.9f);
  82. smallRedBag.add(sp);
  83. sp = new Goods("地图",1,9.9f);
  84. smallRedBag.add(sp);
  85. sp = new Goods("白酒",2,68f);
  86. smallWhiteBag.add(sp);
  87. sp = new Goods("红茶",3,180f);
  88. smallWhiteBag.add(sp);
  89. sp = new Goods("锅",1,380f);
  90. mediumBag.add(sp);
  91. mediumBag.add(smallRedBag);
  92. sp = new Goods("运动鞋",1,198f);
  93. BigBag.add(mediumBag);
  94. BigBag.add(smallWhiteBag);
  95. BigBag.add(sp);
  96. System.out.println("选购的商品有:");
  97. BigBag.show();
  98. System.out.println("商品的总价是"+BigBag.calculation());
  99. }
  100. }

执行的结果如下:

选购的商品有:
锅(数量:1,单价:380.0元)
特产(数量:2,单价:7.9元)
地图(数量:1,单价:9.9元)
白酒(数量:2,单价:68.0元)
红茶(数量:3,单价:180.0元)
运动鞋(数量:1,单价:198.0元)
商品的总价是1279.7

四、组合模式的应用场景

前面分析了组合模式的结构与特点,下面分析它适用的以下应用场景。

  1. 在需要表示一个对象整体与部分的层次结构的场合。
  2. 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合。