H__D  

组合模式

看一个学校院系展示需求 

  编写程序展示一个学校院系结构:

  需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。如图:

  

  传统方案解决学校院系展示,如图:

  

  传统方案解决学校院系展示存在的问题分析:

  1. 将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的

  2. 实际上我们的要求是:在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系,因此这种方案,不能很好实现的管理的操作,比如对学院、系的添加,删除,遍历等

  3. 解决方案:把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树形结构,可以更好的实现管理操作。 => 组合模式

组合模式基本介绍

模式的结构

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

  1. 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成

  2. 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于实现抽象构件角色中声明的公共接口

  3. 树枝构件(Composite)角色:是组合中的分支节点对象,它有子节点。它实现了抽象构件角色中声明的接口,它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法

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

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

  

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

  

下面给出透明式的组合模式的实现代码,与安全式的组合模式的实现代码类似,只要对其做简单修改就可以了。

 1 package composite;
 2 import java.util.ArrayList;
 3  
 4 public class CompositePattern {
 5     
 6     public static void main(String[] args) {
 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  
21 // 抽象构件
22 interface Component {
23     public void add(Component c);
24     public void remove(Component c);
25     public Component getChild(int i);
26     public void operation();
27 }
28  
29  
30 // 树叶构件
31 class Leaf implements Component {
32  
33     private String name;
34     public Leaf(String name) {
35         this.name=name;
36     }
37  
38     public void add(Component c){ } 
39           
40     public void remove(Component c){ }
41    
42     public Component getChild(int i) {
43         return null;
44     }   
45  
46     public void operation() {
47         System.out.println("树叶" + name + ":被访问!"); 
48     }
49 }
50  
51  
52 // 树枝构件
53 class Composite implements Component {
54  
55     private ArrayList<Component> children = new ArrayList<Component>();
56    
57     public void add(Component c) {
58         children.add(c);
59     }   
60  
61     public void remove(Component c) {
62         children.remove(c);
63     } 
64   
65     public Component getChild(int i) {
66         return children.get(i);
67     }   
68  
69     public void operation() {
70         for(Object obj:children) {
71             ((Component)obj).operation();
72         }
73     }    
74 } 

程序运行结果如下:

  1. 树叶1:被访问!
  2. 树叶2:被访问!
  3. 树叶3:被访问!

组合模式解决学校院系展示的应用实例

应用实例要求:

  接口中实现子部件的相关操作,比如增加(add),

  1)编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。

  2)思路分析和图解(类图)

  

  3)代码实现

 1 package com.atguigu.composite;
 2  
 3 public class Client {
 4  
 5     public static void main(String[] args) {
 6  
 7         // 从大到小创建对象学校
 8         OrganizationComponent university = new University("清华大学", " 中国顶级大学 ");
 9         // 创建学院
10         OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 "); 
11         OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 ");
12         // 创建各个学院下面的系(专业)
13         computerCollege.add(new Department("软件工程", " 软件工程不错 "));
14         computerCollege.add(new Department("网络工程", " 网络工程不错 "));
15         computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 "));
16  
17         infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 "));           
18         infoEngineercollege.add(new Department("信息工程", " 信息工程好学 "));
19         // 将学院加入到学校 
20         university.add(computerCollege);         
21         university.add(infoEngineercollege);
22         // university.print();
23         infoEngineercollege.print(); 
24     }
25 }

 

 1 package com.atguigu.composite;
 2 import java.util.ArrayList; 
 3 import java.util.List;
 4  
 5 public class College extends OrganizationComponent {
 6  
 7     // List 中 存放的 Department
 8     List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
 9  
10     // 构造器
11     public College(String name, String des) {
12         super(name, des);
13     }
14  
15     // 重写 add
16     @Override
17     protected void add(OrganizationComponent organizationComponent) {
18         // 将来实际业务中,Colleage 的 add 和 University add 不一定完全一样
19         organizationComponents.add(organizationComponent); 
20     }
21  
22     // 重写 remove
23     @Override
24     protected void remove(OrganizationComponent organizationComponent) {
25         organizationComponents.remove(organizationComponent); 
26     }
27  
28     @Override
29     public String getName() {
30         return super.getName(); 
31     }
32  
33     @Override
34     public String getDes() {
35         return super.getDes(); 
36     }
37  
38     // print 方法,就是输出 University 包含的学院 
39     @Override
40     protected void print() {
41         System.out.println("--------------" + getName() + "--------------");
42         // 遍历 organizationComponents
43         for (OrganizationComponent organizationComponent : organizationComponents) {
44             organizationComponent.print(); 
45         }
46     }
47 }
 1 package com.atguigu.composite;
 2  
 3 public class Department extends OrganizationComponent {
 4  
 5     // 没有集合
 6     public Department(String name, String des) { 
 7         super(name, des);
 8     }
 9  
10     // add , remove 就不用写了,因为他是叶子节点 
11     @Override
12     public String getName() {
13         return super.getName();
14     }
15  
16     @Override
17     public String getDes() {
18         return super.getDes(); 
19     }
20  
21     @Override
22     protected void print() {
23         System.out.println(getName()); 
24     }
25 }

 

 1 package com.atguigu.composite;
 2  
 3 public abstract class OrganizationComponent {
 4  
 5     // 名字 
 6     private String name; 
 7     // 说明
 8     private String des;
 9  
10     protected void add(OrganizationComponent organizationComponent) { 
11         // 默认实现
12         throw new UnsupportedOperationException(); 
13     }
14  
15     protected void remove(OrganizationComponent organizationComponent) { 
16         // 默认实现
17         throw new UnsupportedOperationException(); 
18     }
19  
20     // 构造器
21     public OrganizationComponent(String name, String des) {
22         super();
23         this.name = name; 
24         this.des = des;
25     }
26  
27     public String getName() { 
28         return name;
29     }
30  
31     public void setName(String name) { 
32         this.name = name;
33     }
34  
35     public String getDes() {
36         return des; 
37     }
38  
39     public void setDes(String des) { 
40         this.des = des;
41     }
42  
43     // 方法 print, 做成抽象的, 子类都需要实现 
44     protected abstract void print();
45 }
 1 package com.atguigu.composite;
 2 import java.util.ArrayList; 
 3 import java.util.List;
 4  
 5 // University 就是 Composite , 可以管理 College
 6 public class University extends OrganizationComponent {
 7  
 8     List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
 9  
10     // 构造器
11     public University(String name, String des) {
12         super(name, des);
13     }
14  
15     // 重写 add
16     @Override
17     protected void add(OrganizationComponent organizationComponent) {
18         organizationComponents.add(organizationComponent); 
19     }
20  
21     // 重写 remove
22     @Override
23     protected void remove(OrganizationComponent organizationComponent) {
24         organizationComponents.remove(organizationComponent); 
25     }
26  
27     @Override
28     public String getName() {
29         return super.getName(); 
30     }
31  
32     @Override
33     public String getDes() {
34         return super.getDes(); 
35     }
36  
37     // print 方法,就是输出 University 包含的学院 @Override
38     protected void print() {
39         System.out.println("--------------" + getName() + "--------------");
40         // 遍历 organizationComponents
41         for (OrganizationComponent organizationComponent : organizationComponents) {
42             organizationComponent.print(); 
43         }
44     } 
45 }

组合模式的应用场景

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

  1. 在需要表示一个对象整体与部分的层次结构的场合

  2. 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合

组合模式在 JDK 集合的源码分析

  1)Java 的集合类-HashMap 就使用了组合模式

  2)代码分析+Debug 源码

  

  3)类图

  

组合模式的注意事项和细节

  1. 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题

  2. 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动

  3. 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构

  4. 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式

  5. 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式

组合模式的扩展

  如果对前面介绍的组合模式中的树叶节点和树枝节点进行抽象,也就是说树叶节点和树枝节点还有子节点,这时组合模式就扩展成复杂的组合模式了,如 Java AWT/Swing 中的简单组件 JTextComponent 有子类 JTextField、JTextArea,容器组件 Container 也有子类 Window、Panel。复杂的组合模式的结构图如图 5 所示。

  

 

原文链接:https://blog.csdn.net/qq784515681/article/details/106347131

posted on 2021-03-16 21:03  H__D  阅读(116)  评论(0编辑  收藏  举报