Java设计模式-组合模式

组合模式

  在现实生活中,存在很多“部分-整体”的关系,例如,大学中的部门与学院、总公司中的部门与分公司、学习用品中的书与书包、生活用品中的衣月艮与衣柜以及厨房中的锅碗瓢盆等。在软件开发中也是这样,例如,文件系统中的文件与文件夹、窗体程序中的简单控件与容器控件等。对这些简单对象与复合对象的处理,如果用组合模式来实现会很方便。

组合模式的定义与特点

  组合(Composite)模式的定义:有时又叫作部分-整体模式,它是一种将对象组合成树状的层次结构的模式,用来表示“部分-整体”的关系,使用户对单个对象和组合对象具有一致的访问性。

  组合模式的主要优点有:

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

  其主要缺点是:

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

组合模式的结构与实现

  组合模式的结构不是很复杂,下面对它的结构和实现进行分析。

1. 组合模式的结构

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

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

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

  (1) 透明方式:在该方式中,由于抽象构件声明了所有子类中的全部方法,所以客户端无须区别树叶对象和树枝对象,对客户端来说是透明的。但其缺点是:树叶构件本来没有 Add()、Remove() 及 GetChild() 方法,却要实现它们(空实现或抛异常),这样会带来一些安全性问题。其结构图如图所示。
透明式的组合模式的结构图
  (2) 安全方式:在该方式中,将管理子构件的方法移到树枝构件中,抽象构件和树叶构件没有对子对象的管理方法,这样就避免了上一种方式的安全性问题,但由于叶子和分支有不同的接口,客户端在调用时要知道树叶对象和树枝对象的存在,所以失去了透明性。其结构图如图所示。
安全式的组合模式的结构图

2. 组合模式的实现

  假如要访问集合 c0={leaf1,{leaf2,leaf3}} 中的元素,其对应的树状图如下图所示。
集合c0的树状图
  透明式的组合模式的实现代码:

//TransparentComponentTest.java
package Composite;

import java.util.ArrayList;

/**
 * 透明式的组合模式
 */
public class TransparentComponentTest {

    public static void main(String[] args) {
        TransparentComponent c0 = new TransparentComposite();
        TransparentComponent c1 = new TransparentComposite();
        TransparentComponent leaf1 = new TransparentLeaf("1");
        TransparentComponent leaf2 = new TransparentLeaf("2");
        TransparentComponent leaf3 = new TransparentLeaf("3");
        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);
        c0.operation();
    }

}


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

    public void remove(TransparentComponent c);

    public TransparentComponent getChild(int i);

    public void operation();
}

//树叶构件
class TransparentLeaf implements TransparentComponent {
    private String name;

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

    public void add(TransparentComponent c) {
    }

    public void remove(TransparentComponent c) {
    }

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

    public void operation() {
        System.out.println("树叶" + name + ":被访问!");
    }
}

//树枝构件
class TransparentComposite implements TransparentComponent {
    private ArrayList<TransparentComponent> children = new ArrayList<TransparentComponent>();

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

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

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

    public void operation() {
        for (Object obj : children) {
            ((TransparentComponent) obj).operation();
        }
    }
}

  安全式的组合模式的实现代码:

package Composite;

import java.util.ArrayList;

/**
 * 安全式的组合模式
 */

public class SecurityComponentTest {

    public static void main(String[] args) {
        SecurityComposite c0 = new SecurityComposite();
        SecurityComposite c1 = new SecurityComposite();
        SecurityComponent leaf1 = new SecurityLeaf("1");
        SecurityComponent leaf2 = new SecurityLeaf("2");
        SecurityComponent leaf3 = new SecurityLeaf("3");
        c0.add(leaf1);
        c0.add(c1);
        c1.add(leaf2);
        c1.add(leaf3);
        c0.operation();
    }

}


//抽象构件
interface SecurityComponent {
    public void operation();
}

//树叶构件
class SecurityLeaf implements SecurityComponent {
    private String name;

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

    public void operation() {
        System.out.println("树叶" + name + ":被访问!");
    }
}

//树枝构件
class SecurityComposite implements SecurityComponent {
    private ArrayList<SecurityComponent> children = new ArrayList<SecurityComponent>();

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

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

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

    public void operation() {
        for (Object obj : children) {
            ((SecurityComponent) obj).operation();
        }
    }
}

程序运行结果如下:

树叶1:被访问!
树叶2:被访问!
树叶3:被访问!

组合模式模式应用场景

  
前面分析了组合模式的结构与特点,下面分析它适用的以下应用场景。

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

组合模式的扩展

  组合模式所包含的 4 个角色不是任何时候都要存在的,在有些应用环境下模式是可以简化的,如以下两种情况。

  如果对前面介绍的组合模式中的树叶节点和树枝节点进行抽象,也就是说树叶节点和树枝节点还有子节点,这时组合模式就扩展成复杂的组合模式了,如 Java AWT/Swing 中的简单组件 JTextComponent 有子类 JTextField、JTextArea,容器组件 Container 也有子类 Window、Panel。复杂的组合模式的结构图如下图所示。
复杂的组合模式的结构图

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值