部分整体模式 设计模式——组合模式

开始介绍之前,先分析一个实例:
编写一个程序展示一个学校院系结构,需要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系:如下:
——————**大学**————————
——————计算机学院———————
计算机科学与技术
软件工程
网络工程
——————信息工程学院————————
通信工程
信息工程
针对上述实例,很容易想到写一个学校类,一个学院类继承学校类,一个专业类继承学院类 。对此方法进行分析:
(1)将学院看作是学校的子类,系是学院的子类,实际上是站在组织大小进行分层的,这种方案不能实现较好的管理操作,比如对学院、系的添加删除,遍历等 。
(2)解决方案:将学校、院、系都看作是组织结构,他们之间没有继承关系,而是树形结构,可以更好的实现管理操作,即使用组合模式 。
基本介绍
(1)组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体—部分”的层次关系 。
(2)组合模式依据树形结构来组合对象,用来表示部分以及整体层次 。
(3)这种类型的设计模式属于树形结构模式 。
(4)组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一致的方式处理个别对象以及组合对象 。
原理图:
说明:
(1):这是组合中对象声明接口,在适当的情况下,实现所有类共有的接口默认行为 。用于访问和管理子部件,可以是抽象类或者接口 。
(2)Leaf:在组合中表示叶子节点,叶子节点没有子节点
(3):在组合中表示非叶子节点,用于存储子部件,在中实现子部件的相关操作,比如增加,删除 。
实例分析

部分整体模式  设计模式——组合模式

文章插图
上述实例使用组合模式实现:
// Componentpublic abstract class OrganizationComponent {private String name;private String description;public OrganizationComponent(String name, String description) {this.name = name;this.description = description;}// 不是所有的子类都需要add,remove,因此提供默认的实现protected void add(OrganizationComponent organizationComponent) {}protected void remove(OrganizationComponent organizationComponent) {}// 所有的子类都需要输出信息,因此做成抽象方法protected abstract void print();public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDescription() {return description;}public void setDescription(String description) {this.description = description;}}// 相当于Composite,可以管理Collegepublic class University extends OrganizationComponent {List organizationComponentLists = new ArrayList<>();public University(String name, String description) {super(name, description);}@Overridepublic String getName() {return super.getName();}@Overridepublic String getDescription() {return super.getDescription();}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponentLists.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponentLists.remove(organizationComponent);}@Overrideprotected void print() {System.out.println("---------------" + getName() + "----------------");for (OrganizationComponent organization : organizationComponentLists) {organization.print();}}}public class College extends OrganizationComponent {List organizationComponentLists = new ArrayList<>();public College(String name, String description) {super(name, description);}@Overridepublic String getName() {return super.getName();}@Overridepublic String getDescription() {return super.getDescription();}@Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponentLists.add(organizationComponent);}@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponentLists.remove(organizationComponent);}@Overrideprotected void print() {System.out.println("-----" + getName());for (OrganizationComponent organization : organizationComponentLists) {organization.print();}}}public class Department extends OrganizationComponent {public Department(String name, String description) {super(name, description);}@Overridepublic String getName() {return super.getName();}@Overridepublic String getDescription() {return super.getDescription();}@Overrideprotected void print() {System.out.println(getName());}}// 主方法public static void main(String[] args) {University university = new University("***大学", "NB大学");College computerCollege = new College("计算机学院", "计算机学院");College infoEngineerCollege = new College("信息工程学院", "信息工程");computerCollege.add(new Department("计算机科学与技术", "计科"));computerCollege.add(new Department("软件工程", "软件工程"));infoEngineerCollege.add(new Department("通信工程", "通信"));infoEngineerCollege.add(new Department("电子信息", "电子信息"));university.add(computerCollege);university.add(infoEngineerCollege);university.print();}// 运行结果/**---------------***大学---------------------计算机学院计算机科学与技术软件工程-----信息工程学院通信工程电子信息*/