美文网首页
组合模式

组合模式

作者: 闽越布衣 | 来源:发表于2019-08-04 10:37 被阅读0次

描述

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

简介

安全式组合模式
透明式组合模式

    安全模式的组合模式要求管理聚集的方法只出现在树枝构件类中,而不出现在树叶构件类中。
    透明式的组合模式要求所有的具体构件类,不论树枝构件还是树叶构件,均符合一个固定接口。

角色

  • 抽象构件(Component)角色:这是一个抽象角色,它给参加组合的对象定义出公共的接口及其默认行为,可以用来管理所有的子对象。组合对象通常把它所包含的子对象当做类型为Component的对象。在安全式的组合模式里,构件角色并不定义出管理子对象的方法,这一定义由树枝构件对象给出。
  • 树叶构件(Leaf)角色:树叶对象是没有下级子对象的对象,定义出参加组合的原始对象的行为。
  • 树枝构件(Composite)角色:代表参加组合的有下级子对象的对象。树枝构件类给出所有的管理子对象的方法,如add()、delete()以及get()。

优缺点

优点

  • 组合模式使得客户端代码可以一致地处理对象和对象容器,无需关系处理的单个对象,还是组合的对象容器。
  • 可以更容易地往组合对象中加入新的构件。
  • 将”客户代码与复杂的对象容器结构“解耦。

缺点

  • 使得设计更加复杂。客户端需要花更多时间理清类之间的层次关系。
  • 在使用组合模式时,其叶子和树枝的声明都是实现类,而不是接口,违反了依赖倒置原则。

使用场景

  • 当想表达对象的部分-整体的层次结构时。(部分、整体场景,如树形菜单,文件、文件夹的管理。)
  • 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象时。

示例

安全式组合模式

public interface Component {
    /**
     *  输出组建自身的名称
     */
    void operation(String name);
}
public class Composite implements Component {
    /**
     * 组合对象的名字
     */
    private String name;
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 传入组合对象的名称
     *
     * @param name 组合对象的名称
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法,增加一个子构件对象
     *
     * @param component 子构件对象
     */
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法,返回所有子构件对象
     */
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 输出对象的自身结构
     * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进
     */
    /**
     * 聚集管理方法,删除一个子构件对象
     *
     * @param index 子构件对象的下标
     */
    public void removeComponent(int index) {
        componentList.remove(index);
    }


    /**
     * 输出组建自身的名称
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}

public class Leaf implements Component {
    private String name;

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

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }
}

public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的电脑");
        Composite c1 = new Composite("C盘");
        Composite c2 = new Composite("D盘");

        Leaf leaf1 = new Leaf("文件1");
        Leaf leaf2 = new Leaf("文件2");
        Leaf leaf3 = new Leaf("文件3");
        Leaf leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}

透明式组合模式

public abstract class Component {

    public abstract void operation(String name);

    public void addComponent(Component component) {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

    public List<Component> getComponent() {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

    public void removeComponent(int index) {
        throw new UnsupportedOperationException("叶子节点不支持此功能");
    }

}
public class Composite extends Component {
    /**
     * 组合对象的名字
     */
    private String name;
    /**
     * 用来存储组合对象中包含的子组件对象
     */
    private List<Component> componentList = new ArrayList<>();

    /**
     * 传入组合对象的名称
     *
     * @param name 组合对象的名称
     */
    public Composite(String name) {
        this.name = name;
    }

    /**
     * 聚集管理方法,增加一个子构件对象
     *
     * @param component 子构件对象
     */
    @Override
    public void addComponent(Component component) {
        this.addComponent(component);
    }

    /**
     * 聚集管理方法,返回所有子构件对象
     */
    @Override
    public List<Component> getComponent() {
        return componentList;
    }
    /**
     * 输出对象的自身结构
     * @param preStr 前缀,主要是按照层级拼接空格,实现向后缩进
     */
    /**
     * 聚集管理方法,删除一个子构件对象
     *
     * @param index 子构件对象的下标
     */
    @Override
    public void removeComponent(int index) {
        componentList.remove(index);
    }

    /**
     * 输出组建自身的名称
     */
    @Override
    public void operation(String name) {
        System.out.println(this.name);
        if (this.componentList != null) {
            for (Component c : componentList) {
                c.operation(name);
            }
        }
    }
}
public class Leaf extends Component {
    private String name;

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

    @Override
    public void operation(String name) {
        System.out.println(name + "-" + this.name);
    }

}
public class Client {
    public static void main(String[] args) {
        Composite root = new Composite("我的电脑");
        Composite c1 = new Composite("C盘");
        Composite c2 = new Composite("D盘");

        Component leaf1 = new Leaf("文件1");
        Component leaf2 = new Leaf("文件2");
        Component leaf3 = new Leaf("文件3");
        Component leaf4 = new Leaf("文件4");

        root.addComponent(c1);
        root.addComponent(c2);
        c1.addComponent(leaf1);
        c1.addComponent(leaf2);
        c2.addComponent(leaf3);
        c2.addComponent(leaf4);

        root.operation("test");
    }
}

相关文章

  • 设计模式:组合模式 职责链模式

    组合模式 职责链模式 组合模式 组合模式将对象组合成树形结构,以表示“部分-整体”的层次结构。 在组合模式的树形结...

  • 第4章 结构型模式-组合模式

    一、组合模式简介 二、组合模式的优缺点 三、组合模式的使用场景 、组合模式的实例

  • 组合模式(统一叶子与组合对象)

    目录 从生活场景出发,映射组合模式 组合模式的理论概念 组合模式的实现 组合模式在源码中的应用 组合 “优于” 继...

  • 组合模式

    1. 组合模式 1.1 组合模式的定义 组合模式(Composite): 又称部分-整体模式, 将对象组合成树形结...

  • 组合模式

    设计模式系列7--组合模式 《Objective-c 编程之道 iOS 设计模式解析》 - 组合模式 常见组合模式...

  • 设计模式 | 组合模式及典型应用

    本文的主要内容: 介绍组合模式 示例 组合模式总结 源码分析组合模式的典型应用java.awt中的组合模式Java...

  • 组合模式

    一、组合模式介绍 二、组合模式代码实例

  • 组合模式

    设计模式之组合模式 什么是组合模式? 组合模式允许你将对象组合成树形结构来表现”部分-整体“的层次结构,使得客户以...

  • 15、组合模式(Composite Pattern)

    1. 组合模式 1.1 简介   Composite模式,即组合模式,又叫部分整体模式。Composite模式将对...

  • 组合模式原型解析

    组合模式定义: 组合模式,将对象组合成树形结构以表示“部分-整体”的层次结构,组合模式使得用户对单个对象和组合对象...

网友评论

      本文标题:组合模式

      本文链接:https://www.haomeiwen.com/subject/eatskqtx.html