Java设计模式 - 组合模式

不断学习才是王道

继续踏上学习之路,学之分享笔记

总有一天我也能像各位大佬一样

原创作品,更多关注我CSDN: 一个有梦有戏的人

准备将博客园、CSDN一起记录分享自己的学习心得!!!

分享学习心得,欢迎指正,大家一起学习成长!

简介

组合模式(Composite Pattern),又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

组合模式原理

首先先看一下组合的UML图

Compoent:抽象类或者接口,是组合对象声明的接口,实现所有类的默认行为,用于访问、管理子部件。

Leaf:组合中的叶子节点,最小的类

Composite:非叶子节点,用来操作组合对象,存储子部件。

组合模式实例

接下来用一个例子来学习组合模式,学校有学院,学院下有专业,这就是一层一层的关系,需要在一个页面中展现出那个学校有什么学院,学院下有什么专业。

来看一下例子的类图

①、定义抽象类-Component

定义属性,构造器,getset,操作方法需要默认实现,因为在叶子节点是不需要去实现的,如果定义成抽象类,子类就必须实现了。在定义一个抽象类-打印信息。

package com.lyd.demo.composite;

/**
* @Author: lyd
* @Description: 抽象类
* @Date: 2022-08-30
*/
public abstract class OrganizationComponent {
private String name;
private String description;
public OrganizationComponent(String name, String description) {
this.name = name;
this.description = description;
}
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;
}
// 添加 - 子类不一定需要实现
public void add(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
// 删除 - 子类不一定需要实现
public void remove(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException();
}
// 打印 - 子类必须去实现
public abstract void print();
}

②、定义叶子类和非叶子类

大学类:非叶子,组合 院系类;定义一个数组来存放组合对象,通过重写操作方法对其进行操作。

package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: lyd
* @Description: 学校类 - 继承OrganizationComponent - 组合 院系类
* @Date: 2022-08-30
*/
public class University extends OrganizationComponent {
// 组合 College 类
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public University(String name, String description) {
super(name, description);
}
/**
* 重写add
*/
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重写remove
*/
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 打印包含学院的信息
@Override
public void print() {
System.out.println("< " + getName() + " >");
// 将所有学院信息打印出来
for (OrganizationComponent o : organizationComponents) {
o.print();
}
}
}

学院类:非叶子,组合Department类,定义一个数组来存放组合对象,通过重写操作方法对其进行操作。

package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
import java.util.ArrayList;
import java.util.List;
/**
* @Author: lyd
* @Description: 学院类
* @Date: 2022-08-30
*/
public class College extends OrganizationComponent {
// 组合 Department 类
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public College(String name, String description) {
super(name, description);
}
/**
* 重写add
*/
@Override
public void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
/**
* 重写remove
*/
@Override
public void remove(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 打印包含学院的信息
@Override
public void print() {
System.out.println("< " + getName() + " >");
// 将所有专业信息打印出来
for (OrganizationComponent o : organizationComponents) {
o.print();
}
}
}

专业类:叶子节点,没有组合的集合,所以不需要进行操作,只需要进行输出打印。

package com.lyd.demo.compose;
import com.lyd.demo.composite.OrganizationComponent;
/**
* @Author: lyd
* @Description: 专业类
* @Date: 2022-08-30
*/
public class Department extends OrganizationComponent {
// 已经没有集合了
public Department(String name, String description) {
super(name, description);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDescription() {
return super.getDescription();
}
// 叶子节点,就不需要add和remove
@Override
public void print() {
System.out.println(getName());
}
}

③、测试

package com.lyd.demo.test;
import com.lyd.demo.compose.College;
import com.lyd.demo.compose.Department;
import com.lyd.demo.compose.University;
import com.lyd.demo.composite.OrganizationComponent;
/**
* @Author: lyd
* @Description: 测试
* @Date: 2022-08-30
*/
public class ComposeTest {
public static void main(String[] args) {
// 创建大学
OrganizationComponent ZheJiangUniversity = new University("浙江大学", "人才之地");
// 创建学院
OrganizationComponent ComputerCollege = new College("计算机科学与技术学院", "-->计算机科学与技术学院");
OrganizationComponent OpticalCollege = new College("光电科学与工程学院", "-->光电科学与工程学院");
// 创建专业
Department ComputerDepartment = new Department("计算机科学与技术", "--计算机科学与技术");
Department SoftWareDepartment = new Department("软件工程", "--软件工程");
Department OpticalDepartment = new Department("光电信息科学与工程", "--光电信息科学与工程");
// 添加专业
ComputerCollege.add(ComputerDepartment);
ComputerCollege.add(SoftWareDepartment);
OpticalCollege.add(OpticalDepartment);
// 添加学院
ZheJiangUniversity.add(ComputerCollege);
ZheJiangUniversity.add(OpticalCollege);
// 打印所有
ZheJiangUniversity.print();
System.out.println("*******************************");
// 打印学院
ComputerCollege.print();
System.out.println("*******************************");
// 打印院系
ComputerDepartment.print();
}
}

组合可以理解成是层层相套。

运行结果:

通俗的讲,组合模式就是将对象组合到非类中,在非子类中进行对他们的操作,有种层层相套的感觉,可以通过打断点的形式一步一步了解。

创作不易,可能有些语言不是很通畅,如有错误请指正,感谢观看!记得一键三连哦!

最新文章

  1. java 异步处理
  2. js算法之最常用的排序
  3. oracle大数据量。表分区提示查询效率
  4. GOF业务场景的设计模式-----责任链模式
  5. System.Net.WebException : The remote server returned an error: (415) UNSUPPORTED MEDIA TYPE
  6. Spring3系列6 - Spring 表达式语言(Spring EL)
  7. HDU3996 Gold Mine(最大权闭合子图)
  8. ks使用lvm分区,ks启动
  9. python中的异常处理
  10. Android Phonebook编写联系人UI加载及联系人保存流程(一)
  11. Android UI -- 内容简介
  12. 安装Windows2008操作系统 - 初学者系列 - 学习者系列文章
  13. vi/vim 使用
  14. Joseph POJ - 1012 约瑟夫环递推
  15. springboot中配置文件application.properties的配置详情,数据源配置
  16. Docker 轻量级图形管理软件 Portainer
  17. C# 数值类型和无穷大
  18. 在系统中使用Bean Validation验证参数
  19. android 权限
  20. NET 异步多线程,THREAD,THREADPOOL,TASK,PARALLEL

热门文章

  1. UiPath循环活动Do While的介绍和使用
  2. Linux系列之查找命令
  3. 虚拟机启动时报’A start job is running for /etc/rc.local .. Compatibility错误。
  4. HashSet存储自定义类型元素和LinkedHashSet集合
  5. 密度峰值聚类算法原理+python实现
  6. STC8H开发(十三): I2C驱动DS3231高精度实时时钟芯片
  7. Java中运算符和方法的区别
  8. PaddleOCR系列(一)--环境搭建
  9. javaweb 03: jsp
  10. 用户认证(Authentication)进化之路:由Basic Auth到Oauth2再到jwt