组合模式(Composite) : 组合模式有时又叫部分-整体模式在处理类似树形结构的问题时比较方便
使用场景:将多个对象组合在一起进行操作,常用于表示树形结构中
作者是个二吊子,如果描述有误请指出.
使用场景
我们可能会操作到一些属性结构的数据,常见的是省市区,多级菜单.它们会具有较多的类似行为(方法).在这个时候我们常见的操作是进行多级菜单定义或者通用一个类来做.如果进行多级菜单定义可能会导致类爆炸.通用一个类的话就可能是灵活度不高,比如有少数方法的差异性处理是做不到的.后期扩展会很麻烦.以此引出组合模式.
组合模式
uml图
这里我们用大学,学院,系来进行距离,网上通用模板
OrganizationComponent : 抽象接口,提供公共的操作方法,用于访问和管理子部件
Department: 叶子结点,没有子节点了,作为最底层
College,University: 容器对象义有枝节点行为,用来存储子部件,在OrganizationComponent 接口中实现与子部件通用操作
/**
* @Description: 抽象类根级
* @Author: gaofan
* @Date: 2020/3/23 13:52
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public abstract class OrganizationComponent {
private String name;
private String desc;
protected void add(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException("不支持操作");
}
protected void remove(OrganizationComponent organizationComponent) {
throw new UnsupportedOperationException("不支持操作");
}
protected abstract void print();
public OrganizationComponent(String name, String desc) {
this.name = name;
this.desc = desc;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
}
/**
* @Description: 学院,容器对象-非叶子节点
* @Author: gaofan
* @Date: 2020/3/23 14:02
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class College extends OrganizationComponent{
List<OrganizationComponent> organizationComponentList = new ArrayList<>();
public College(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentList.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponentList.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("-------次顶层--------"+getName() + "------------");
for(OrganizationComponent organizationComponent: organizationComponentList){
organizationComponent.print();
}
}
}
/**
* @Description: 大学,容器对象-非叶子节点
* @Author: gaofan
* @Date: 2020/3/23 13:56
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponentList = new ArrayList<>();
public University(String name, String desc) {
super(name, desc);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponentList.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponentList.remove(organizationComponent);
}
@Override
protected void print() {
System.out.println("-------顶层--------"+getName() + "------------");
for(OrganizationComponent organizationComponent: organizationComponentList){
organizationComponent.print();
}
}
}
/**
* @Description: 系
* @Author: gaofan
* @Date: 2020/3/23 14:05
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Department extends OrganizationComponent {
public Department(String name, String desc) {
super(name, desc);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDesc() {
return super.getDesc();
}
@Override
protected void print() {
System.out.println("----叶子节点--------"+getName() + "------------");
}
}
/**
* @Description: TODO
* @Author: gaofan
* @Date: 2020/3/23 14:07
* @Copyright: 2020 www.withu.top Inc. All rights reserved.
**/
public class Client {
public static void main(String [] args){
OrganizationComponent university = new University("宜宾学院", "中国次顶级学校");
OrganizationComponent college = new College("物理与电子工程学院","老学院");
OrganizationComponent college1 = new College("经管学院","新学院");
college.add(new Department("电子信息工程","还行"));
college.add(new Department("物理系","不错"));
college1.add(new Department("国际贸易","一般"));
college1.add(new Department("财经贸易","不知道"));
university.add(college);
university.add(college1);
university.print();
}
}
类型: 组合模式
原理: 树形结构具有较多通用方法时可用于此类.减少使用继承的手法之一.通过定义接口或者抽象类对容器对象,叶子对象行为(方法)进行抽象,可根据各自行为自行实现(差异化).容器对象通过组合的方式持有容器对象或者叶子对象.对其进行操作.
优点: 减少耦合性,复用性增强,灵活性较强
本文借鉴尚硅谷Java设计模式,韩顺平图解java,传送门