组合模式
前言
- 首先通过一个问题来发现组合模式是怎样来的
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e8555e2bb3d9a700abb8bd5912796503.png#pic_center)
- 传统的解决方法是通过系继承学院,学院继承学校来实现的
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/2bebd67189227e57d9c8c4825ef8d7a0.png#pic_center)
- 而这种方式就有很大的问题:
- 将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的,实际上我们的要求 是 在 一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系, 因此这种方案,不能很好实现的管理的操作,比如对学院,系的添加,删除,遍历等
- 解决方案:把学校、院、系 都看做是组织结构,他们之间没有继承的关系,而是
一个树形结构,可以更好的实现管理操作。这就是组合模式
基本介绍
- 组合模式( Composite Pattern ),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体 部分”的层次关系。
- 组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
- 这种类型的设计模式属于结构型模式。
- 组合模式使得用户对单个对象和组合对象的访问具有一致性, 即 :组合能让客户以一致的方式处理个别对象以及组合对象
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/bfa3a56d2234cbac0397750b59067d40.png#pic_center)
具体应用实现
- 类图
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0a24b8775dc72f34a0322883621ac786.png#pic_center)
- 代码展示:
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;
}
protected abstract void print();
}
/University 就是 Composite , 可以管理College
public class University extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public University(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
public class College extends OrganizationComponent {
List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();
public College(String name, String des) {
super(name, des);
}
@Override
protected void add(OrganizationComponent organizationComponent) {
organizationComponents.add(organizationComponent);
}
@Override
protected void remove(OrganizationComponent organizationComponent) {
organizationComponents.remove(organizationComponent);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println("--------------" + getName() + "--------------");
for (OrganizationComponent organizationComponent : organizationComponents) {
organizationComponent.print();
}
}
}
public class Profession extends OrganizationComponent{
public Profession(String name, String des) {
super(name, des);
}
@Override
public String getName() {
return super.getName();
}
@Override
public String getDes() {
return super.getDes();
}
@Override
protected void print() {
System.out.println(getName());
}
}
public class Client {
public static void main(String[] args) {
OrganizationComponent university = new University("清华大学", " 中国顶级大学 ");
OrganizationComponent computerCollege = new College("计算机学院", " 计算机学院 ");
OrganizationComponent infoEngineercollege = new College("信息工程学院", " 信息工程学院 ");
computerCollege.add(new Profession("软件工程", " 软件工程不错 "));
computerCollege.add(new Profession("网络工程", " 网络工程不错 "));
computerCollege.add(new Profession("计算机科学与技术", " 计算机科学与技术是老牌的专业 "));
infoEngineercollege.add(new Profession("通信工程", " 通信工程不好学 "));
infoEngineercollege.add(new Profession("信息工程", " 信息工程好学 "));
university.add(computerCollege);
university.add(infoEngineercollege);
computerCollege.print();
}
}
总结
- 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题 。
- 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
- 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
- 需要遍历组织机构,或者处理的对象具有树形结构时 , 非常适合使用组合模式
- 要求较高的抽象性, 如果节点和叶子有很多差异性的话 ,比如很多方法和属性都不一样不适合使用组合 模式