设计模式之【组合模式】,树形结构的完美解决方案

一、什么是组合模式

组合模式(Composite Pattern)也称为整体-部分(Part-Whole)模式,它的宗旨是通过将单个对象(叶子节点)和组合对象(树枝节点)用相同的接口进行表示,使得客户对单个对象和组合对象的使用具有一致性,属于结构型模式。

组合模式依据树形结构来组合对象,用来表示部分以及整体层次。`

GOF24原文:将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用具有一致性。

组合模式的设计思路,与其说是一种设计模式,倒不如说是对业务场景的一种数据结构和算法的抽象。其中,数据可以表示成树这种数据结构,业务需求可以通过在树上的递归遍历算法来实现。组合模式,将一组对象组织成树形结构,将单个对象和组合对象都看做树中的节点,以统一处理逻辑,并且它利用树形结构的特点,递归地处理每个子树,依次简化代码实现。使用组合模式的前提在于,你的业务场景必须能够表示成树形结构。所以,组合模式的应用场景也比较局限,它并不是一种很常用的设计模式

1、组合模式三大角色

组合模式一般用来描述整体与部分的关系,它将对象组织到树形结构中,最顶层的节点称为根节点,根节点下面可以包含树枝节点和叶子节点,树枝节点下面又可以包含树枝节点和叶子节点。如下图所示:
在这里插入图片描述
由上图可以看出,其实根节点和树枝节点本质上是同一种数据类型,可以作为容器使用:而叶子节点与树枝节点在语义上不属于同一种类型,但是在组合模式中,会把树枝节点和叶子节点认为是同一种数据类型(用同一接口定义),让它们具备一致行为。这样,在组合模式中,整个树形结构中的对象都是同一种类型,带来的一个好处就是客户无需辨别树枝节点还是叶子节点,而是可以直接进行操作,给客户使用带来极大的便利。

组合模式包含3个角色:

  • 抽象根节点(Component):定义系统各层次对象的共有方法和属性,可以预先定义一些默认的行为和属性;
  • 树枝节点(Composite):定义树枝节点的行为,存储子节点,组合树枝节点和叶子节点形成一个树形结构;
  • 叶子节点(Leaf):叶子节点对象,其下再无分支,是系统层次遍历的最小单位。

组合模式在代码实现上,有两种不同的方式,分别是透明组合模式和安全组合模式

2、组合模式应用场景

当子系统与其内各个对象层次呈现树形结构时,可以使用组合模式让子系统内各个对象层次的行为操作具备一致性。客户端使用该子系统内任意一个层次对象时,无需进行区分,直接使用通用操作即可,为客户端的使用提供了便捷。

(1)希望客户端可以忽略组合对象与单个对象的差异时;
(2)对象层次具备整体和部分,呈树形结构。

常用的组合模式场景:
树形菜单;操作系统目录结构;公司组织架构等等。
在这里插入图片描述

3、组合模式注意事项和细节

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

二、透明组合模式

透明组合模式是把所有公共方法都定义在Component中,这样做的好处是客户端无需分辨是叶子节点(Leaf)和树枝节点(Composite),它们具备完全一致的接口。其UML类图如下所示:
在这里插入图片描述

1、学院院系案例

我们以一个学校的院系结构为例,一个学校有多个学院,一个学院有多个系。

先定义抽象根节点:

// 抽象根节点
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;
	}
	// 省略 get  set
	
	//方法print, 做成抽象的, 子类都需要实现
	protected abstract void print();
	
}

大学:

//University 就是 Composite , 可以管理College
public class University extends OrganizationComponent {
    
    
	// 定义学院集合 College
	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 {
    
    

	//没有集合
	
	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());
	}
}

测试类:

public class Client {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		
		//从大到小创建对象 学校
		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();
		university.print();
	}

}

最终的UML类图如下:
在这里插入图片描述

2、透明组合模式总结

透明组合模式把所有公共方法都定义在Component中,这样做的好处是客户端无需分辨是叶子节点(Leaf)和树枝节点(Composite),它们具备完全一致的接口;缺点是叶子节点(Leaf)会继承得到一些它所不需要(管理子类操作的方法)的方法,这与设计模式 接口隔离原则相违背。

三、安全组合模式

安全组合模式是只规定系统各个层次的最基础的一致行为,而把组合(树节点)本身的方法(管理子类对象的添加、删除等)放到自身当中。其UML类图如下所示:
在这里插入图片描述

1、linux目录系统案例

文件系统有两大层次:文件夹和文件。其中,文件夹能容纳文件,为树枝节点;文件为叶子节点。由于目录系统层次较少,且树枝节点(文件夹)结构相对稳定,而文件其实可以有很多类型,所以这里我们选择使用安全组合模式来实现目录系统,可以避免为叶子类型(文件)引入冗余方法。

// 顶层抽象
public abstract class Directory {
    
    

    protected String name;

    public Directory(String name) {
    
    
        this.name = name;
    }

    public abstract void show();
}
// 目录
public class Folder extends Directory {
    
    
    private List<Directory> dirs;

    private Integer level;

    public Folder(String name,Integer level) {
    
    
        super(name);
        this.level = level;
        this.dirs = new ArrayList<Directory>();
    }

    @Override
    public void show() {
    
    
        System.out.println(this.name);
        for (Directory dir : this.dirs) {
    
    
            //控制显示格式
            if(this.level != null){
    
    
                for(int  i = 0; i < this.level; i ++){
    
    
                    //打印空格控制格式
                    System.out.print("  ");
                }
                for(int  i = 0; i < this.level; i ++){
    
    
                    //每一行开始打印一个+号
                    if(i == 0){
    
     System.out.print("+"); }
                    System.out.print("-");
                }
            }
            //打印名称
            dir.show();
        }
    }

    public boolean add(Directory dir) {
    
    
        return this.dirs.add(dir);
    }

    public boolean remove(Directory dir) {
    
    
        return this.dirs.remove(dir);
    }

    public Directory get(int index) {
    
    
        return this.dirs.get(index);
    }

    public void list(){
    
    
        for (Directory dir : this.dirs) {
    
    
            System.out.println(dir.name);
        }
    }

}
// 文件
public class File extends Directory {
    
    

    public File(String name) {
    
    
        super(name);
    }

    @Override
    public void show() {
    
    
        System.out.println(this.name);
    }

}
// 测试类
class Test {
    
    
    public static void main(String[] args) {
    
    

        System.out.println("============安全组合模式===========");

        File qq = new File("QQ.exe");
        File wx = new File("微信.exe");

        Folder office = new Folder("办公软件",2);

        File word = new File("Word.exe");
        File ppt = new File("PowerPoint.exe");
        File excel = new File("Excel.exe");

        office.add(word);
        office.add(ppt);
        office.add(excel);

        Folder wps = new Folder("金山软件",3);
        wps.add(new File("WPS.exe"));
        office.add(wps);

        Folder root = new Folder("根目录",1);
        root.add(qq);
        root.add(wx);
        root.add(office);

        System.out.println("----------show()方法效果-----------");
        root.show();

        System.out.println("----------list()方法效果-----------");
        root.list();
    }
}

我们看一下UML类图:
在这里插入图片描述

2、安全组合模式总结

安全组合模式的好处是接口定义职责清晰,符合设计模式 单一职责原则和 接口隔离原则;缺点是客户需要区分树枝节点(Composite)和叶子节点(Leaf),这样才能正确处理各个层次的操作,客户端无法依赖抽象(Component),违背了设计模式 依赖倒置原则。

四、源码中使用的组合模式

1、HashMap

Map就是一个抽象构建(同时这个构建中只支持键值对的存储格式),而HashMap是一个中间构建,HashMap中的Node节点就是叶子节点。

public class HashMap<K,V> extends AbstractMap<K,V>
    implements Map<K,V>, Cloneable, Serializable {
    
    
    // ...
	transient Node<K,V>[] table;
	// ...
}	

static class Node<K,V> implements Map.Entry<K,V> {
    
    
    final int hash;
    final K key;
    V value;
    Node<K,V> next;
}

其putAll方法的入参就是Map:

public void putAll(Map<? extends K, ? extends V> m) {
    
    
    putMapEntries(m, true);
}

2、ArrayList

ArrayList的addAll方法,其参数也是父类Collection:

public boolean addAll(Collection<? extends E> c) {
    
    
    Object[] a = c.toArray();
    int numNew = a.length;
    ensureCapacityInternal(size + numNew);  // Increments modCount
    System.arraycopy(a, 0, elementData, size, numNew);
    size += numNew;
    return numNew != 0;
}

3、MyBatis

MyBatis中解析各种Mapping文件中的SQL,设计了一个非常关键的类叫做SqlNode,xml中的每一个Node都会解析为一个SqlNode对象,最后把所有的SqlNode拼装到一起就成了一条完整的SQL语句,它的顶层设计非常简单。来看源代码:

public interface SqlNode {
    
    
  boolean apply(DynamicContext context);
}

Apply方法会根据传入的参数context,参数解析该SqlNode所记录的SQL片段,并调用DynamicContext.appendSql()方法将解析后的SQL片段追加到DynamicContext的SqlBuilder中保存。当SQL节点下所有的SqlNode完成解析后,可以通过DynamicContext.getSql()获取一条完整的SQL语句。

我们看一下类图:
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/A_art_xiang/article/details/130606421