设计模式 -- 组合模式(Composite Pattern)

2024-08-30 10:04

本文主要是介绍设计模式 -- 组合模式(Composite Pattern),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1 问题引出

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

2 基本介绍

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

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

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

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

3 原理结构

3.1 图解

3.2 说明(组合模式的角色及职责)

  1. Component :这是组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component 子部件, Component 可以是抽象类或者接口。

  2. Leaf : 在组合中表示叶子节点,叶子节点没有子节点,继承或实现了抽象构件。

  3. Composite :非叶子节点, 用于存储子部件, 在 Component 接口中实现 子部件的相关操作,比如增加(add), 删除。

4 应用实例

4.1 类图

4.2 代码实现

public class Client {public static void main(String[] args) {//  从大到小创建对象 学校OrganizationComponent university = new University("清华大学", " 中国顶级大学 ");//  创建 学院OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 ");OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 ");//  创建各个学院下面的系(专业)computerCollege.add(new Department("软件工程", " 软件工程不错 "));computerCollege.add(new Department("网络工程", " 网络工程不错 "));computerCollege.add(new Department("计算机科学与技术", " 计算机科学与技术是老牌的专业 "));// infoEngineercollege.add(new Department("通信工程", " 通信工程不好学 "));infoEngineercollege.add(new Department("信息工程", " 信息工程好学 "));//  将学院加入到 学校university.add(computerCollege);//  university.add(infoEngineercollege);//  university.print(); infoEngineercollege.print();}}public abstract class OrganizationComponent {private String name; //  名字private String des; //  说明protected void add(OrganizationComponent organizationComponent) {//  默认实现throw new UnsupportedOperationException();}protected void remove(OrganizationComponent organizationComponent) {//  默认实现throw new UnsupportedOperationException();}//  构造器public OrganizationComponent(String name, String des) {super();this.name = name;this.des = des;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDes() {return des;}public void setDes(String des) {this.des = des;}//  方法 print, 做成抽象的, 子类都需要实现protected abstract void print();}public class College extends OrganizationComponent {//  List 中 存放的 DepartmentList<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();//  构造器public College(String name, String des) {super(name, des);}//  重写 add @Overrideprotected void add(OrganizationComponent organizationComponent) {//  将来实际业务中,Colleage 的 add 和 University add 不一定完全一样organizationComponents.add(organizationComponent);}//  重写 remove @Overrideprotected void remove(OrganizationComponent organizationComponent) {//  organizationComponents.remove(organizationComponent);}@Overridepublic String getName() {return super.getName();}@Overridepublic String getDes() {return super.getDes();}//  print 方法,就是输出 University 包含的学院@Overrideprotected void print() {System.out.println("--------------" + getName() + "	");//  遍历 organizationComponentsfor (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}}public class Department extends OrganizationComponent {//  没有集合public Department(String name, String des) {super(name, des);}//  add , remove 就不用写了,因为他是叶子节点@Overridepublic String getName() {return super.getName();}@Overridepublic String getDes() {return super.getDes();}@Overrideprotected void print() {System.out.println(getName());}
}// University 就是 Composite , 可以管理 College
public class University extends OrganizationComponent {List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();//  构造器public University(String name, String des) {super(name, des);}//  重写 add @Overrideprotected void add(OrganizationComponent organizationComponent) {organizationComponents.add(organizationComponent);}//  重写 remove@Overrideprotected void remove(OrganizationComponent organizationComponent) {organizationComponents.remove(organizationComponent);}@Overridepublic String getName() {return super.getName();}@Overridepublic String getDes() {return super.getDes();}//  print 方法,就是输出 University 包含的学院@Overrideprotected void print() {System.out.println("--------------" + getName() + "	");//  遍历 organizationComponentsfor (OrganizationComponent organizationComponent : organizationComponents) {organizationComponent.print();}}}

5 优缺点

5.1 优点

  1. 简化客户端代码:客户端可以统一处理所有类型的节点,无须关心处理的是单个对象还是组合对象。
  2. 高扩展性:当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动,满足开闭原则。
  3. 结构清晰:树形结构清晰反映了部分与整体的关系,易于理解和维护。如果需要遍历组织机构,或者处理的对象具有树形结构时, 非常适合使用组合模式。

5.2 缺点

  1. 设计复杂:涉及多个类和接口,新手理解和实施可能较困难。
  2. 限制困难:难以用继承方法增加新功能或限制容器中的构件类型。如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式。
  3. 性能问题:处理深层树形结构时可能导致性能下降,如栈溢出。

6 应用场景

  • 文件系统管理:文件作为树叶节点,文件夹作为树枝节点,通过组合模式实现文件系统的层级管理。
  • 图形界面组件:在GUI中,可以将简单控件和容器控件组合起来,统一管理。
  • 组织架构模拟:公司部门、书籍目录、学校院系等组织结构可以通过组合模式进行模拟和操作。

7 总结

        综上所述,组合模式提供了一种高效的管理树形结构的方式,适用于各种具有层次结构的场景。虽然其设计和实现相对复杂,但其带来的好处是显著的。在选择使用组合模式时,需要根据具体需求和上下文环境来决定其适用性。

这篇关于设计模式 -- 组合模式(Composite Pattern)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1120506

相关文章

Linux系统配置NAT网络模式的详细步骤(附图文)

《Linux系统配置NAT网络模式的详细步骤(附图文)》本文详细指导如何在VMware环境下配置NAT网络模式,包括设置主机和虚拟机的IP地址、网关,以及针对Linux和Windows系统的具体步骤,... 目录一、配置NAT网络模式二、设置虚拟机交换机网关2.1 打开虚拟机2.2 管理员授权2.3 设置子

SpringBoot如何通过Map实现策略模式

《SpringBoot如何通过Map实现策略模式》策略模式是一种行为设计模式,它允许在运行时选择算法的行为,在Spring框架中,我们可以利用@Resource注解和Map集合来优雅地实现策略模式,这... 目录前言底层机制解析Spring的集合类型自动装配@Resource注解的行为实现原理使用直接使用M

C#原型模式之如何通过克隆对象来优化创建过程

《C#原型模式之如何通过克隆对象来优化创建过程》原型模式是一种创建型设计模式,通过克隆现有对象来创建新对象,避免重复的创建成本和复杂的初始化过程,它适用于对象创建过程复杂、需要大量相似对象或避免重复初... 目录什么是原型模式?原型模式的工作原理C#中如何实现原型模式?1. 定义原型接口2. 实现原型接口3

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

Java实现状态模式的示例代码

《Java实现状态模式的示例代码》状态模式是一种行为型设计模式,允许对象根据其内部状态改变行为,本文主要介绍了Java实现状态模式的示例代码,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来... 目录一、简介1、定义2、状态模式的结构二、Java实现案例1、电灯开关状态案例2、番茄工作法状态案例

解读静态资源访问static-locations和static-path-pattern

《解读静态资源访问static-locations和static-path-pattern》本文主要介绍了SpringBoot中静态资源的配置和访问方式,包括静态资源的默认前缀、默认地址、目录结构、访... 目录静态资源访问static-locations和static-path-pattern静态资源配置

hdu4869(逆元+求组合数)

//输入n,m,n表示翻牌的次数,m表示牌的数目,求经过n次操作后共有几种状态#include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<queue>#include<set>#include<map>#include<stdio.h>#include<stdlib.h>#includ

在JS中的设计模式的单例模式、策略模式、代理模式、原型模式浅讲

1. 单例模式(Singleton Pattern) 确保一个类只有一个实例,并提供一个全局访问点。 示例代码: class Singleton {constructor() {if (Singleton.instance) {return Singleton.instance;}Singleton.instance = this;this.data = [];}addData(value)

模版方法模式template method

学习笔记,原文链接 https://refactoringguru.cn/design-patterns/template-method 超类中定义了一个算法的框架, 允许子类在不修改结构的情况下重写算法的特定步骤。 上层接口有默认实现的方法和子类需要自己实现的方法

【iOS】MVC模式

MVC模式 MVC模式MVC模式demo MVC模式 MVC模式全称为model(模型)view(视图)controller(控制器),他分为三个不同的层分别负责不同的职责。 View:该层用于存放视图,该层中我们可以对页面及控件进行布局。Model:模型一般都拥有很好的可复用性,在该层中,我们可以统一管理一些数据。Controlller:该层充当一个CPU的功能,即该应用程序