美文网首页
设计模式之组合(Composite)

设计模式之组合(Composite)

作者: ikonan | 来源:发表于2018-08-03 08:23 被阅读6次

概述

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

从定义中可以看出,组合模式用来表示部分与整体的层次结构(类似于树结构),而且也可以使用户对单个对象(叶子节点)以及组合对象(非叶子节点)的使用具有一致性,一致性的意思就是说,这些对象都拥有相同的接口。

UML类图

组合模式UML类图

代码实例

透明式的组合模式

在Component中声明所有来管理子对象的方法,其中包括Add,Remove等。这样实现Component接口的所有子类都具备了Add和Remove方法。这样做的好处是叶节点和枝节点对于外界没有区别,它们具备完全一致的接口。

/**
 * 一个抽象构件,声明一个接口用于访问和管理Component的子部件
 */
public abstract class Component {

    protected String name;
    public Component(String name) {
        this.name = name;
    }

    /**
     * 增加一个节点
     * @param component
     */
    public abstract void add(Component component);

    /**
     * 移除一个节点
     * @param component
     */
    public abstract void remove(Component component);

    /**
     * 显示层级结构
     * @param level
     */
    public abstract void display(int level);
}

叶子节点

import java.util.Collections;

/**
 * 叶子节点
 */
public class Leaf extends Component{

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

    /**
     * 由于叶子节点没有子节点,所以Add和Remove方法对它来说没有意义,
     * 但它继承自Component,这样做可以消除叶节点和枝节点对象在抽象层次的区别,
     * 它们具备完全一致的接口。
     * @param component
     */
    @Override
    public void add(Component component) {
        System.out.println("can not add a component to a leaf.");
    }

    /**
     * 实现它没有意义,只是提供了一个一致的调用接口
     * @param component
     */
    @Override
    public void remove(Component component) {
        System.out.println("can not add a component to a leaf.");
    }

    @Override
    public void display(int level) {
        String str = String.join("", Collections.nCopies(level,"-"));
        System.out.println(str +name);
    }
}

定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作
 */
public class Composite extends Component{

    //一个子对象集合,用来存储其下属的枝节点和叶节点
    private List<Component> children = new ArrayList<Component>();

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

    /**
     * 增加子节点
     * @param component
     */
    @Override
    public void add(Component component) {
        children.add(component);
    }

    /**
     * 移除子节点
     * @param component
     */
    @Override
    public void remove(Component component) {
        children.remove(component);
    }


    @Override
    public void display(int level) {
        String str = String.join("", Collections.nCopies(level,"-"));
        System.out.println(str +name);

        for (Component component: children) {
            component.display(level+2);
        }

    }
}

测试代码


public class CompositeTest {

    public static void main(String[] args) {

        // 生成树根,并为其增加两个叶子节点
        Component root = new Composite("root");
        root.add(new Leaf("leaf a in root"));
        root.add(new Leaf("leaf b in root"));

        //为根增加两个枝节点
        Component branchx = new Composite("branch x in root");
        Component branchy = new Composite("branch y in root");
        root.add(branchx);
        root.add(branchy);

        // 为BranchX增加页节点
        branchx.add(new Leaf("leaf a in branch x"));

        //BranchX增加枝节点
        Component branchz = new Composite("branch z in branch x");
        branchx.add(branchz);

        // 为BranchY增加叶节点
        branchy.add(new Leaf("leaf in branch y"));

        // 为BranchZ增加叶节点
        branchz.add(new Leaf("leaf in branch z"));

        root.display(1);
    }
}

运行结果:
-root
---leaf a in root
---leaf b in root
---branch x in root
-----leaf a in branch x
-----branch z in branch x
-------leaf in branch z
---branch y in root
-----leaf in branch y

缺点:客户端对叶节点和枝节点是一致的,但叶节点并不具备Add和Remove的功能,因而对它们的实现是没有意义的

安全式组合模式

在Component中不去声明Add和Remove方法,那么子类的Leaf就不需要实现它,而是在Composit声明所有用来管理子类对象的方法。

一个抽象构件,声明一个接口用于访问和管理Component的子部件

/**
 * 一个抽象构件,声明一个接口用于访问和管理Component的子部件
 */
public abstract class Component {

    protected String name;
    public Component(String name) {
        this.name = name;
    }

    /**
     * 显示层级结构
     * @param level
     */
    public abstract void display(int level);
}

叶子节点

import java.util.Collections;

/**
 * 叶子节点
 */
public class Leaf extends Component{

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

    @Override
    public void display(int level) {
        String str = String.join("", Collections.nCopies(level,"-"));
        System.out.println(str +name);
    }
}

定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 定义有枝节点的行为,用来存储部件,实现在Component接口中对子部件有关的操作
 */
public class Composite extends Component{

    //一个子对象集合,用来存储其下属的枝节点和叶节点
    private List<Component> children = new ArrayList<Component>();

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

    /**
     * 增加子节点
     * @param component
     */
    public void add(Component component) {
        children.add(component);
    }

    /**
     * 移除子节点
     * @param component
     */
    public void remove(Component component) {
        children.remove(component);
    }


    @Override
    public void display(int level) {
        String str = String.join("", Collections.nCopies(level,"-"));
        System.out.println(str +name);

        for (Component component: children) {
            component.display(level+2);
        }
    }
}

测试类

public class CompositeTest {

    public static void main(String[] args) {

        // 生成树根,并为其增加两个叶子节点
        Composite root = new Composite("root");
        root.add(new Leaf("leaf a in root"));
        root.add(new Leaf("leaf b in root"));

        //为根增加两个枝节点
        Composite branchx = new Composite("branch x in root");
        Composite branchy = new Composite("branch y in root");
        root.add(branchx);
        root.add(branchy);

        // 为BranchX增加页节点
        branchx.add(new Leaf("leaf a in branch x"));

        //BranchX增加枝节点
        Composite branchz = new Composite("branch z in branch x");
        branchx.add(branchz);

        // 为BranchY增加叶节点
        branchy.add(new Leaf("leaf in branch y"));

        // 为BranchZ增加叶节点
        branchz.add(new Leaf("leaf in branch z"));

        root.display(1);
    }
}

运行结果:
-root
---leaf a in root
---leaf b in root
---branch x in root
-----leaf a in branch x
-----branch z in branch x
-------leaf in branch z
---branch y in root
-----leaf in branch y

缺点:叶节点无需在实现Add与Remove这样的方法,但是对于客户端来说,必须对叶节点和枝节点进行判定,为客户端的使用带来不便。

总结

优点:

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

缺点:

  • 使得设计更加复杂。客户端需要花更多时间理清类之间的层次关系。(这个是几乎所有设计模式所面临的问题)。

相关文章

网友评论

      本文标题:设计模式之组合(Composite)

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