Java设计模式之组合模式

1. 组合模式

1.1 定义、优缺点、适用场景

定义:Composite Pattern,有时又叫作整体-部分(Part-Whole)模式,它是一种将对象组合成树状的层次结构的模式,使用户对单个对象和组合对象具有一致的访问性(对不同数据类型的,需要用接口进行统一)。例如树可以有树叶和树枝,树枝又可以有树叶和树枝;公司有不同的部门,不同的部门有不同的组

优点

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

缺点

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

适用场景

  • 在需要表示一个对象整体与部分的层次结构的场合
  • 要求对用户隐藏组合对象与单个对象的不同,用户可以用统一的接口使用组合结构中的所有对象的场合

1.2 模式的结构

结构

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

1.3 透明式的组合模式实现

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

import java.util.ArrayList;

public class CompositeTest {

    public static void main(String[] args) {
        Component mainBranch = new Branch();
        // 给主树枝添加树叶
        Component leaf1 = new Leaf("leaf1");
        mainBranch.add(leaf1);
        // 给主树枝添加树枝
        Component childBranch = new Branch();
        mainBranch.add(childBranch);

        // 给子树枝添加树叶
        Component leaf2 = new Leaf("leaf2");
        childBranch.add(leaf2);

        mainBranch.operation();
    }
}


// 抽象构件
interface Component {
    public void add(Component c);

    public void remove(Component c);

    public Component getChild(int index);

    public void operation();
}

// 树叶构件
class Leaf implements Component {
    private String name;

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

    public void add(Component c) {
    }

    public void remove(Component c) {
    }

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

    public void operation() {
        System.out.println("我是树叶" + name);
    }
}

// 树枝构件
class Branch 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();
        }
    }
}

运行程序,结果如下:

我是树叶leaf1
我是树叶leaf2

1.4 安全式的组合模式实现

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

import java.util.ArrayList;

public class CompositeTest {

    public static void main(String[] args) {
        // 树枝构件类型为Branch类型,以便获取管理子类操作的方法
        Branch mainBranch = new Branch();
        // 给主树枝添加树叶
        Component leaf1 = new Leaf("leaf1");
        mainBranch.add(leaf1);
        // 给主树枝添加树枝
        Branch childBranch = new Branch();
        mainBranch.add(childBranch);

        // 给子树枝添加树叶
        Component leaf2 = new Leaf("leaf2");
        childBranch.add(leaf2);

        mainBranch.operation();
    }
}


// 抽象构件, 只保留层次的公共行为
interface Component {

    public void operation();
}

// 树叶构件
class Leaf implements Component {
    private String name;

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

    public void operation() {
        System.out.println("我是树叶" + name);
    }
}

// 树枝构件
class Branch 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();
        }
    }
}

运行程序,结果如下:

我是树叶leaf1
我是树叶leaf2

1.5 组合模式的扩展

将树叶构件和树枝构件进行抽象,然后对树叶构建提供不同的实现,对树枝构件也提供不同的实现,就扩展成复杂的组合模式

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值