基本介绍:
    1、组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体-部分”的层次关系
    2、组合模式依据树形结构来组合对象,用来表示部分以及整体层次
    3、这种类型的设计模式属于结构型模式
    4、组合模式使得用户对单个对象和组合对象的访问具有一致性,即组合能让客户以一致的方式处理个别对象以及组合对象

    组合模式类图:
    image.png

    角色说明:
    1、Component:组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或接口
    2、Leaf:在组合中表示叶子节点,叶子节点没有叶子节点
    3、Composite:非叶子节点,用于存储子部件,在Component接口中实现子部件的相关操作,比如增加(add)、删除等。

    组合模式解决这样的问题,当我们的要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。

    代码示例:

    1. /**
    2. * 抽象类
    3. */
    4. @Data
    5. public abstract class OrganizationComponent {
    6. private String name;
    7. private String des;
    8. public OrganizationComponent(String name, String des){
    9. super();
    10. this.name = name;
    11. this.des = des;
    12. }
    13. protected void add(OrganizationComponent organizationComponent){
    14. //默认实现
    15. throw new UnsupportedOperationException();
    16. }
    17. protected void remove(OrganizationComponent organizationComponent){
    18. //默认实现
    19. throw new UnsupportedOperationException();
    20. }
    21. protected abstract void print();
    22. }
    23. /**
    24. * 实现类大学
    25. */
    26. public class University extends OrganizationComponent {
    27. List<OrganizationComponent> organizationComponentList = new ArrayList<>();
    28. public University(String name, String des) {
    29. super(name, des);
    30. }
    31. @Override
    32. protected void add(OrganizationComponent organizationComponent) {
    33. organizationComponentList.add(organizationComponent);
    34. }
    35. @Override
    36. protected void remove(OrganizationComponent organizationComponent) {
    37. organizationComponentList.remove(organizationComponent);
    38. }
    39. @Override
    40. protected void print() {
    41. System.out.println("<"+ getName() +">");
    42. for (OrganizationComponent organizationComponent : organizationComponentList) {
    43. organizationComponent.print();
    44. }
    45. }
    46. }
    47. /**
    48. * 实现类学院
    49. */
    50. public class College extends OrganizationComponent {
    51. List<OrganizationComponent> organizationComponentList = new ArrayList<>();
    52. public College(String name, String des) {
    53. super(name, des);
    54. }
    55. @Override
    56. protected void add(OrganizationComponent organizationComponent) {
    57. organizationComponentList.add(organizationComponent);
    58. }
    59. @Override
    60. protected void remove(OrganizationComponent organizationComponent) {
    61. organizationComponentList.remove(organizationComponent);
    62. }
    63. @Override
    64. protected void print() {
    65. System.out.println("--"+ getName() +"--");
    66. for (OrganizationComponent organizationComponent : organizationComponentList) {
    67. organizationComponent.print();
    68. }
    69. }
    70. }
    71. /**
    72. * 实现类专业
    73. */
    74. public class Department extends OrganizationComponent {
    75. List<OrganizationComponent> organizationComponentList = new ArrayList<>();
    76. public Department(String name, String des) {
    77. super(name, des);
    78. }
    79. @Override
    80. protected void print() {
    81. System.out.println(getName());
    82. }
    83. }
    84. /**
    85. * 调用
    86. */
    87. public class Client {
    88. public static void main(String[] args) {
    89. OrganizationComponent university = new University("清华大学", "国内一流大学");
    90. OrganizationComponent computerCollege = new College("计算机学院", "计算机学院不错");
    91. OrganizationComponent informationCollege = new College("信息学院", "信息学院不错");
    92. computerCollege.add(new Department("计算机1专业","计算机1专业不错"));
    93. computerCollege.add(new Department("计算机2专业","计算机2专业不错"));
    94. informationCollege.add(new Department("信息1专业","信息1专业不错"));
    95. informationCollege.add(new Department("信息2专业","信息2专业不错"));
    96. university.add(computerCollege);
    97. university.add(informationCollege);
    98. university.print();
    99. }
    100. }

    打印信息如下:

    1. <清华大学>
    2. --计算机学院--
    3. 计算机1专业
    4. 计算机2专业
    5. --信息学院--
    6. 信息1专业
    7. 信息2专业

    组合模式的注意事项:

    1. 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
    2. 具有较强的扩展性。当要更改组合对象时,只需要调整内部的层次关系,客户端不用做出任何改动
    3. 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
    4. 需要遍历组织机构或者处理的对象具有树形结构时,非常适合使用组合模式
    5. 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式