0
点赞
收藏
分享

微信扫一扫

一篇文章掌握组合模式


一.案例引入

1.案例描述

一篇文章掌握组合模式_树形结构

2.传统方式

传统思想是让学院继承学校,让系继承学院,他们之间是继承关系。

传统方式评价

一篇文章掌握组合模式_ide_02

解决方案

把学校、院、系都看做是组织结构,他们之间没有继承的关系,而是一个树型结构,可以更好的实现管理操作。这就是组合模式

二.组合模式

1.基本介绍

组合模式,又叫部分整体模式,它创建了对象组的树形结构,将对象组合成树状结构以表示“整体·部分”的层次关系。

组合模式依据树形结构来组合对象,用来表示部分以及整体层次。组合模式使得用户对单个对象和组合对象的访问具有一致性,即组合能让客户以一致的方式处理个别对象以及组合对象。

2.原理类图

一篇文章掌握组合模式_组合模式_03

原理说明

一篇文章掌握组合模式_设计模式_04

3.组合模式解决的问题

当我们的要处理的对象可以生成一棵树形结构,而我们要对树上的节点和叶子进行操作时,它能够提供一致的方式,而不用考虑它是节点还是叶子。

4.组合模式解决案例问题的UML类图

一篇文章掌握组合模式_ide_05

5.具体代码

OrganizationComponent

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) {
this.name = name;
this.des = des;
}

//这个方法下面的子类都需要实现
protected abstract void print();

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

import java.util.ArrayList;
import java.util.List;

//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);
}

//输出University所包含的学院
@Override
protected void print() {
System.out.println(getName());
for(OrganizationComponent organizationComponent:organizationComponents){
organizationComponent.print();
}
}

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

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

College

import java.util.ArrayList;
import java.util.List;

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) {
organizationComponents.add(organizationComponent);
}

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

//输出University所包含的学院
@Override
protected void print() {
System.out.println(getName());
for(OrganizationComponent organizationComponent:organizationComponents){
organizationComponent.print();
}
}

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

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

Department

public class Department extends OrganizationComponent{
public Department(String name, String des) {
super(name, des);
}
//add和remove无需再写,因为它是叶子节点,无需管理其他单位了
@Override
protected void print() {
System.out.println(getName());
}

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

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

Client

public class Client {
public static void main(String[] args) {
//从大到小创建对象
OrganizationComponent university = new University("清华大学", "中国顶尖大学");

//创建学院
OrganizationComponent college = new College("计算机学院", "计算机学院");

//创建专业
college.add(new Department("计算机科学与技术","计算机科学与技术专业好"));

//将学院加入到学校
university.add(college);

university.print();
}
}

清华大学
计算机学院
计算机科学与技术

三.组合模式总结

优缺点总结

优点

①简化客户端操作,客户端只需要面对一致的对象而不用考虑整体部分或者节点叶子的问题
②具有较强的扩展性,当我们要更改组合对象时,我们只需要调整内部的层次关系,客户端不用做出任何改动
③方便创建出复杂的层次结构,客户端不用理会组合里面的组成细节,容易添加节点或者叶子从而创建出复杂的树形结构
④需要遍历组织机构,或者处理的对象具有树形结构时,非常适合使用组合模式

缺点

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


举报

相关推荐

0 条评论