zoukankan      html  css  js  c++  java
  • 8.组合模式

    组合模式

    一、应用实例

    编写程序展示一个学校院系结构:要在一个页面中展示学校的院系组成,一个学校有多个学院,一个学院有多个系。

    二、组合模式(Composite Pattern)

    1.基本介绍

    1)组合模式,又叫部分整体模式,将对象组合成树状结构。

    2)组合模式使用户对单个对象和组合对象的访问具有一致性

    2.原理类图

    1)Component:这是组合中对象声明接口,在适当情况下,实现所有类共有的接口默认行为,用于访问和管理Component子部件,Component可以是抽象类或者接口

    2)Leaf:在组合中表示叶子节点,叶子节点没有子节点。

    3)Composite:非叶子节点,用于存储子部件,

    3.组合模式解决学校院系展示的实例

    1)分析类图

    1. 代码实现
    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();
    	}
    }
    
    public abstract class OrganizationComponent {
    
    	private String name; // 名字
    	private String des; // 说明
    
    	//方法print, 做成抽象的, 子类都需要实现
    	protected abstract void print();
    
    	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;
    	}
    }
    
    //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();
    		}
    	}
    }
    
    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 {
    
    	//没有集合
    	//add , remove 就不用写了,因为他是叶子节点
    
    	public Department(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());
    	}
    }
    

    四、组合模式在JDK集合的源码分析

    1)HashMap使用了组合模式

    2)源码分析

    五、组合模式的注意事项

    1)具有较强的扩展性。当我们要更改组合对象时,只要调整内部的层次关系,客户端不用做出任何改动。

    2)需要遍历组织机构,或者处理的对象具有属性结构时,非常适合使用组合模式

    3)要求较高的抽象性,如果节点和叶子有很多差异性的话,比如很多方法和属性都不一样,不适合使用组合模式

  • 相关阅读:
    令人抓狂的redis和rediscluster Python驱动包的安装
    基于Docker的Consul集群实现服务发现
    应用中引入缓存层后,缓存和持久化层数据一致性的思考
    Redis中几个简单的概念:缓存穿透/击穿/雪崩,别再被吓唬了
    Redis Cluster 自动化安装,扩容和缩容
    mysql 8.0 忘记root密码后重置
    MySQL 8.0部分弃用的参数整理
    MySQL Online DDL与DML并发阻塞关系总结
    MySQL Error Log 文件丢失导致The server quit without updating PID file启动失败的场景
    MySQL删除大表时潜在的问题(drop table,truncate table)
  • 原文地址:https://www.cnblogs.com/chao-zjj/p/11300615.html
Copyright © 2011-2022 走看看