设计模式之组合模式

详细内容请移步至语雀文档:点击跳转

一、组合模式概述

1.1 什么是组合模式(Composite Pattern)

例如大学中的学院与专业、生活用品中的衣服与衣柜、文件系统中的文件与文件夹等。我们在对这些简单对象与复合对象的处理时,如果采用组合模式来实现会更加方便。组合模式有时又叫作整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,一般用来描述整体与部分的关系,使用户对单个对象和组合对象具有一致的访问性,属于结构型设计模式

1.2 组合模式的优缺点

1.2.1 组合模式的优点:

  • 组合模式使得客户端代码可以一致地处理单个对象和组合对象,无须关心自己处理的是单个对象,还是组合对象,这简化了客户端代码;
  • 更容易在组合体内加入新的对象,客户端不会因为加入了新的对象而更改源代码,满足“开闭原则”;

1.2.2 组合模式的缺点:

  • 设计较复杂,客户端需要花更多时间理清类之间的层次关系;
  • 不容易限制容器中的构件;
  • 不容易用继承的方法来增加构件的新功能;

1.3 组合模式的结构

组合模式包含以下主要角色。

  1. 抽象构件(Component)角色:它的主要作用是为树叶构件和树枝构件声明公共接口,并实现它们的默认行为。在透明式的组合模式中抽象构件还声明访问和管理子类的接口;在安全式的组合模式中不声明访问和管理子类的接口,管理工作由树枝构件完成。(总的抽象类或接口,定义一些通用的方法,比如新增、删除)
  2. 树叶构件(Leaf)角色:是组合中的叶节点对象,它没有子节点,用于继承或实现抽象构件。
  3. 树枝构件(Composite)角色 / 中间构件:是组合中的分支节点对象,它有子节点,用于继承和实现抽象构件。它的主要作用是存储和管理子部件,通常包含 Add()、Remove()、GetChild() 等方法。

组合模式分为透明式的组合模式和安全式的组合模式。

1.3.1 透明方式

在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。其结构图如图所示。

透明式的组合模式的结构图

1.3.2 安全方式

在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。其结构图如图所示。

安全式的组合模式的结构图

1.4 组合模式的实习案例

假如要访问集合 c0={leaf1,{leaf2,leaf3}} 中的元素,其对应的树状图如图所示。

集合c0的树状图

1.4.1 透明方式进行实现

/**
 * 抽象构件
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public interface Component {

    public void add(Component c);

    public void remove(Component c);

    public Component getChild(int i);

    public void operation();
}
/**
 * 叶子结点
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class LeafComponent implements Component{

    private String name;

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

    public void add(Component c) {}

    public void remove(Component c) {}

    public Component getChild(int i) {
        return null;
    }

    public void operation() {
        System.out.println("树叶"+ name + "被访问!");
    }
}
/**
 * 树枝结点
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class Composite implements Component {

    private ArrayList<Component> children = new ArrayList<Component>();

    public void add(Component c) {
        children.add(c);
    }

    public void remove(Component c) {
        children.remove(c);
    }

    public Component getChild(int i) {
        return children.get(i);
    }

    public void operation() {
        for (Object obj : children) {
            ((Component)obj).operation();
        }
    }
}
/**
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class DemoTest {
    public static void main(String[] args) {

        Composite c0 = new Composite();
        Composite c1 = new Composite();
        LeafComponent leaf1 = new LeafComponent("leaf1");
        LeafComponent leaf2 = new LeafComponent("leaf2");
        LeafComponent leaf3 = new LeafComponent("leaf3");

        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);

        c0.operation();
    }
}
运行结果:
树叶leaf1被访问!
树叶leaf2被访问!
树叶leaf3被访问!

1.4.1 安全组合方式进行实现

/**
 * 抽象构件
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public interface Component {

    public void operation();
}
/**
 * 叶子结点
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class LeafComponent implements Component{

    private String name;

    public LeafComponent(String name) {
        this.name = name;
    }
    public void operation() {
        System.out.println("树叶"+ name + "被访问!");
    }
}
/**
 * 树枝结点
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class Composite implements Component {

    private ArrayList<Component> children = new ArrayList<Component>();

    public void add(Component c) {
        children.add(c);
    }

    public void remove(Component c) {
        children.remove(c);
    }

    public Component getChild(int i) {
        return children.get(i);
    }

    public void operation() {
        for (Object obj : children) {
            ((Component)obj).operation();
        }
    }
}
/**
 * @author Mr. squirrel
 * @date 2021/3/28
 */
public class DemoTest {
    public static void main(String[] args) {

        Composite c0 = new Composite();
        Composite c1 = new Composite();
        LeafComponent leaf1 = new LeafComponent("leaf1");
        LeafComponent leaf2 = new LeafComponent("leaf2");
        LeafComponent leaf3 = new LeafComponent("leaf3");

        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);

        c0.operation();
    }
}
运行结果:
树叶leaf1被访问!
树叶leaf2被访问!
树叶leaf3被访问!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值