11.组合模式

学校院系展示需求

看一个学校院系展示需求:
编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系。如图:
在这里插入图片描述
传统方案解决学校院系展示
在这里插入图片描述
传统方案解决学校院系展示存在的问题分析

  1. 将学院看做是学校的子类,系是学院的子类,这样实际上是站在组织大小来进行分层次的
  2. 实际上我们的要求是 :在一个页面中展示出学校的院系组成,一个学校有多个学院,一个学院有多个系, 因此这种方案,不能很好实现 的管理的操作,比如对学院、系的添加,删除,遍历等
  3. 解决方案:把学校、院、系 都看做是组织结构,他们之间没有继承的关系而是一个树形结构,可以更好的实现管理操作。 ==> 组合模式

组合模式

基本介绍

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

原理类图
在这里插入图片描述
对原理结构图的说明-即(组合模式的角色及职责)
在这里插入图片描述
解决的问题

  1. 组合模式解决这样的问题,当我们的要处理的对象可以生成一颗树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子

  2. 对应的示意图
    在这里插入图片描述
    应用实例

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

  4. 代码实现

//Component角色
public abstract class OrganizationComponent {

	private String name; // 名字
	private String des; // 说明
	
	protected void add(OrganizationComponent organizationComponent) {
		//默认实现  叶子节点(就是不能再分的,最下面的节点,这里是系)不需要重写add等方法,
		// 所以这里要加默认方法,基于适配该方法对所有子类中实现和不实现2种情况的考虑
		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, 做成抽象的, 子类都需要实现
	//不管是叶子节点还是非叶子节点都有这个方法的实现,和add,remove方法不同,和add,remove方法出发点不一样的
	protected abstract void print();
}

//其中一个Composite(合成物)角色 root下面的一层节点
//University 就是 Composite 这个角色 , 可以管理College
public class University extends OrganizationComponent {//大学下面是学院,都是非叶子节点

	//将学院组合进来
	List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

	// 构造器
	public University(String name, String des) {
		super(name, des);
	}

	// 重写add
	@Override
	protected void add(OrganizationComponent organizationComponent) {
		//目的是将学院组合到学校里面来
		organizationComponents.add(organizationComponent);
	}

	// 重写remove
	@Override
	protected void remove(OrganizationComponent organizationComponent) {
		organizationComponents.remove(organizationComponent);
	}

	@Override
	public String getName() {
		return super.getName();
	}

	@Override
	public String getDes() {
		return super.getDes();
	}

	// print方法,就是输出University 包含的学院,包含的子节点,非最后的节点
	@Override
	protected void print() {
		System.out.println("--------------" + getName() + "--------------");
		//遍历 organizationComponents 
		for (OrganizationComponent organizationComponent : organizationComponents) {
			organizationComponent.print();
		}
	}

}
//其中一个Composite(合成物)角色  root下面的下面的一层节点
//这个代表学院,是学校的下一个非叶子节点,但不是最后的叶子节点
public class College extends OrganizationComponent {

	//List 中 存放的Department,这里聚合的系
	List<OrganizationComponent> organizationComponents = new ArrayList<OrganizationComponent>();

	// 构造器
	public College(String name, String des) {
		super(name, des);
	}

	// 重写add
	@Override
	protected void add(OrganizationComponent organizationComponent) {
		//  将来实际业务中,Colleage 的 add 和  University add 业务逻辑不一定完全一样
		organizationComponents.add(organizationComponent);
	}

	// 重写remove
	@Override
	protected void remove(OrganizationComponent organizationComponent) {
		organizationComponents.remove(organizationComponent);
	}

	@Override
	public String getName() {
		return super.getName();
	}

	@Override
	public String getDes() {
		return super.getDes();
	}

	// print方法,就是输出University 包含的学院
	@Override
	protected void print() {
		System.out.println("--------------" + getName() + "--------------");
		//遍历 organizationComponents 
		for (OrganizationComponent organizationComponent : organizationComponents) {
			organizationComponent.print();
		}
	}
}
//叶子节点
//这个是最后的节点,代表系,后面没有节点了
public class Department extends OrganizationComponent {

	//没有集合
	
	public Department(String name, String des) {
		super(name, des);
	}
	
	//add , remove 就不用写了,因为他是叶子节点
	
	@Override
	public String getName() {
		return super.getName();
	}
	
	@Override
	public String getDes() {
		return super.getDes();
	}
	
	@Override
	protected void print() {
		System.out.println(getName());
	}
}
//client
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();
	}
}

  1. 图解(类图)
    在这里插入图片描述
    组合模式在JDK 集合的源码分析
  2. Java的集合类-HashMap就使用了组合模式
  3. 代码分析+Debug 源码
    在这里插入图片描述
    在这里插入图片描述
public class Composite {
	public static void main(String[] args) {
		
		//说明
		//1. Map 就是一个抽象的构建 (类似我们的Component)
		//2. HashMap是一个中间的构建(Composite), 实现/继承了相关方法
		//   put, putall 
		//3. Node 是 HashMap的静态内部类,类似Leaf叶子节点, 这里就没有put, putall方法了
		//   static class Node<K,V> implements Map.Entry<K,V>
		
		Map<Integer,String> hashMap=new HashMap<Integer,String>();
		hashMap.put(0, "东游记");//直接存放叶子节点(Node)
		
		Map<Integer,String> map=new HashMap<Integer,String>();
		map.put(1, "西游记");
		map.put(2, "红楼梦"); //..
		hashMap.putAll(map);
		System.out.println(hashMap);
	}
}

类图:
在这里插入图片描述
组合模式的注意事项和细节

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

猜你喜欢

转载自blog.csdn.net/weixin_43719015/article/details/104578877