好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java设计模式之组合模式深入刨析

1.基本介绍

1)组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示[整体-部分]的层次关系

2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次

3)这种类型的设计模式属于结构型模式

4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象

2.结构

组合模式主要包含三种角色:

抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预定义一些默认行为和属性 树枝节点(Composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构 叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位

3.组合模式解决的问题

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

2)对应的示意图

4.组合模式解决学校院系展示

1)应用实例要求

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

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

3)代码实现

Component组合对象声明接口

?

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

package com.zte;

public abstract class OrganizationComponent {

     private String name; // 名字

     private String des; // 说明

     public String getName() {

         return name;

     }

     public String getDes() {

         return des;

     }

     protected void add(OrganizationComponent organizationComponent) {

         // 默认实现

         throw new UnsupportedOperationException();

     }

     protected void remove(OrganizationComponent organizationComponent) {

         // 默认实现

         throw new UnsupportedOperationException();

     }

     // 构造器

     public OrganizationComponent(String name, String des) {

         this .name = name;

         this .des = des;

     }

     // 方法print,抽象方法

     protected abstract void print();

}

Composite非叶子节点

?

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

package com.zte;

import java.util.ArrayList;

import java.util.List;

// University 就是 Composite,可以管理College

public class University extends OrganizationComponent {

     List<OrganizationComponent> organizationComponentList = new ArrayList<>();

     // 构造器

     public University(String name, String des) {

         super (name, des);

     }

     //重写add

     @Override

     protected void add(OrganizationComponent organizationComponent) {

         organizationComponentList.add(organizationComponent);

     }

     // 重写remove

     @Override

     protected void remove(OrganizationComponent organizationComponent) {

         organizationComponent.remove(organizationComponent);

     }

     @Override

     protected void print() {

         System.out.println( "==========" + getName() + "=========" );

         for (OrganizationComponent organizationComponent : organizationComponentList) {

             organizationComponent.print();

         }

     }

     @Override

     public String getName() {

         return super .getName();

     }

     @Override

     public String getDes() {

         return super .getDes();

     }

}

Composite非叶子节点

?

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

package com.zte;

import java.util.ArrayList;

import java.util.List;

public class College extends OrganizationComponent {

     // list中存放department

     List<OrganizationComponent> organizationComponentList = new ArrayList<>();

     public College(String name, String des) {

         super (name, des);

     }

     //重写add

     @Override

     protected void add(OrganizationComponent organizationComponent) {

         organizationComponentList.add(organizationComponent);

     }

     // 重写remove

     @Override

     protected void remove(OrganizationComponent organizationComponent) {

         organizationComponent.remove(organizationComponent);

     }

     @Override

     protected void print() {

         System.out.println( "==========" + getName() + "=========" );

         for (OrganizationComponent organizationComponent : organizationComponentList) {

             organizationComponent.print();

         }

     }

     @Override

     public String getName() {

         return super .getName();

     }

     @Override

     public String getDes() {

         return super .getDes();

     }

}

Leaf叶子节点

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

package com.zte;

public class Department extends OrganizationComponent {

     public Department(String name, String des) {

         super (name, des);

     }

     // add和remove方法就不需要再写了

     @Override

     protected void print() {

         System.out.println( "===========" + getName() + "=========" );

     }

     @Override

     public String getName() {

         return super .getName();

     }

     @Override

     public String getDes() {

         return super .getDes();

     }

}

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

package com.zte;

public class Client {

     public static void main(String[] args) {

         // 创建大学

         OrganizationComponent university = new University( "清华大学" , "中国最好的大学" );

         // 创建学院

         OrganizationComponent college1 = new College( "计算机学院" , "计算机学院" );

         OrganizationComponent college2 = new College( "信息工程学院" , "信息工程学院" );

         // 创建各个学院下面的系

         college1.add( new Department( "软件工程" , "软件工程" ));

         college1.add( new Department( "网络工程" , "网络工程" ));

         college1.add( new Department( "计算机科学与技术" , "老牌专业" ));

         college2.add( new Department( "通信工程" , "通信工程" ));

         college2.add( new Department( "信息工程" , "信息工程" ));

         // 将学院添加到学校中

         university.add(college1);

         university.add(college2);

         // 打印大学底下的所有院系

         university.print();

         // 打印学院底下的所有系

         college1.print();

     }

}

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

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

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

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

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

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

到此这篇关于Java设计模式之组合模式深入刨析的文章就介绍到这了,更多相关Java组合模式内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

原文链接:https://blog.csdn.net/qq_40742428/article/details/124782155

查看更多关于Java设计模式之组合模式深入刨析的详细内容...

  阅读:14次