0
点赞
收藏
分享

微信扫一扫

设计模式——组合模式

模式介绍

  • 组合模式(Composite Pattern),又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体_部分”的层次关系。
  • 组合模式依据树形结构来组合对象,用来表示部分以及整体层次。
  • 这种类型的设计模式属于结构型模式。
  • 组合模式使得用户对单个对象和组合对象的访问具有一致性,即:组合能让客户以一-致的方式处理个别对象以及组合对象

UML类图

image.png

类图解析:

  • Component :这是组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理。Component子部件, Component可以是抽象类或者接口。
  • Leaf:在组合中表示叶子节点,叶子节点没有子节点
  • Composite :非叶子节点,用于存储子 部件,在 Component接口中实现子部件的相关操作,比如增加(add),删除(delete)。

组合模式案例

背景介绍:一所大学包含多个学院,学院又包含多个学科专业
image.png

OrganizationComponent(抽象类)

public abstract class OrganizationComponent {
private String name;
private String des;

/**
* 默认实现
* @param o
*/

protected void add(OrganizationComponent o) {
throw new UnsupportedOperationException("该方法不支持");
}

protected void remove(OrganizationComponent o) {
throw new UnsupportedOperationException("该方法不支持");
}

public OrganizationComponent(String name, String des) {
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();
}

Department(专业类)

public class Department extends OrganizationComponent{
public Department(String name, String des) {
super(name, des);
}

@Override
protected void print() {
System.out.println(getName());
}
}

College(学院类)

public class College extends OrganizationComponent {
List<OrganizationComponent> departments = new ArrayList<>();

public College(String name, String des) {
super(name, des);
}

@Override
protected void add(OrganizationComponent o) {
departments.add(o);
}

@Override
protected void remove(OrganizationComponent o) {
departments.remove(o);
}

@Override
protected void print() {
System.out.println("-----------" + getName() + "-----------");
departments.forEach(OrganizationComponent::print);
}
}

University(大学类)

public class University extends OrganizationComponent {
List<OrganizationComponent> colleges = new ArrayList<>();

public University(String name, String des) {
super(name, des);
}

@Override
protected void add(OrganizationComponent o) {
colleges.add(o);
}

@Override
protected void remove(OrganizationComponent o) {
colleges.remove(o);
}

@Override
protected void print() {
System.out.println("-----------" + getName() + "-----------");
colleges.forEach(OrganizationComponent::print);
}
}

Client(测试类)

public class Client {
public static void main(String[] args) {

// 创建大学
University university = new University("清华大学", "中国顶级大学");

// 计算机学院
College computerCollege = new College("计算机学院", "计算机学院");
College 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();

}
}

实现结果:

image.png

组合模式的注意事项和细节

  • 简化客户端操作。客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题。
  • 具有较强的扩展性。当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动。
  • 方便创建出复杂的层次结构。客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构。
  • 需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式
  • 要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式。
举报

相关推荐

0 条评论