设计模式15-组合模式

一 组合模式的定义

         组合模式也叫合成模式,有时又叫部分-整体模式,主要是用来描述部分与整体的关系,定义:将对象组合成树形结构以表示“部分-整体”的层次结构,使得用户对单个对象和组合对象的使用一致性。

       组合模式通用类图:


   Component抽象构建角色:定义参加组合对象的共有方法和属性,可以定义一些默认的行为或属性。

Leaf叶子构件:叶子对象,其下再也没有其他的分支,也就是遍历的最小单位。

Composite树枝构件:树枝对象,它的作用是组合树枝节点和叶节点形成一个树形结构。

下面来看看组合模式的通用源代码:

抽象构件:

package com.jack.component;

/**
 * Created by jack on 2018/1/26.
 * 抽象构件
 */
public abstract class Component {
    //个体和整体都具有的共享
    public void doSomething(){
        //编写业务逻辑
        System.out.println("abstract do something");
    }
}

   树枝构件:

package com.jack.component;

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

/**
 * Created by jack on 2018/1/26.
 */
public class Composite extends Component {
    //构建容器
    private List<Component> componentList = new ArrayList<>();

    /**
     * 增加一个叶子构件或者树枝构件
     * @param component
     */
    public void add(Component component) {
        this.componentList.add(component);
    }

    /**
     * 删除一个叶子构件或者树枝构件
     * @param component
     */
    public void remove(Component component){
        this.componentList.remove(component);
    }

    /**
     * 获得分支下所有叶子构件和树枝构件
     * @return
     */
    public List<Component> getChildren(){
        return this.componentList;
    }

}

  树叶构件:

package com.jack.component;

/**
 * Created by jack on 2018/1/26.
 */
public class Leaf extends Component {
    @Override
    public void doSomething() {
        //覆盖写父类的方法
        System.out.println("this is leaf ");
    }
}

测试主类:

package com.jack.component;

/**
 * Created by jack on 2018/1/26.
 */
public class Client {
    public static void main(String[] args) {
        //创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        //创建一个树枝构件
        Composite branch = new Composite();
        //创建一个叶子节点
        Leaf leaf = new Leaf();
        root.add(branch);
        branch.add(leaf);
        display(root);
    }

    public static void display(Composite root) {
        for (Component component : root.getChildren()) {
            if (component instanceof Leaf) {//叶子节点
                component.doSomething();
            } else {
                //树枝节点
                display((Composite) component);

            }
        }
    }

}

二 组合模式的应用

1 组合模式的优点

1)高层模块调用简单

     一棵树行机构的所有节点都是Component,局部和整体对调用者来说没有任何区别,也就是说,高层模块不必关心自己处理的是单个对象还是整个组合结构,简化了高层模块的代码。

2)节点自由增加

      使用了组合模式后,我们可以看看,如果想增加一个树枝节点,树叶节点是不是很容易,只要找到它的父节点就成了,非常容易扩展,符合开闭原则,对以后
的维护非常有利

2 组合模式的缺点

       组合模式有一个非常明显的缺点,看到我们在场景类中定义,直接使用了实现类,这在面向接口编程上是很不恰当的,与
依赖倒置原则冲突,读者在使用的时候要考虑清楚,它限制了你接口的影响范围

3 组合模式的使用场景

1)维护和展示部分-整体关系的场景,如树形菜单,文件和文件夹管理
2)从一个整体中能够独立出部分模块或功能的场景

4 组合模式的注意事项

只要是树形结构,就要考虑使用组合模式,这个一定要记住,只要是要体现局部和整体的关系的时候,而且这种关系还可能比较深,考虑一下组合模式吧


三 透明的组合模式

    组合模式有两种不同的实现:透明模式和安全模式,上面讲的是安全模式,那透明模式是什么样子了?透明模式的通用类图如下:


         透明模式是把用了组合使用的方法放到抽象类中,比如add,remove等方法。不管叶子对象还是树枝对象都有相同的结构,通过判断是getChildren的返回值确认是叶子节点还是树枝节点,如果处理不当,这个会在运行期间出现问题,不是很建议的方式;安全模式就不同了,它是把树枝节点和树叶节点彻底分开,树枝节点单独拥有用来组合的方法,这种方法比较安全,上面的例子使用了安全模式。

       由于透明模式的使用也比较多,下面实现组合模式的透明方式,代码如下:

抽象构建:

package com.jack.component;

import java.util.List;

/**
 * Created by jack on 2018/1/26.
 * 抽象构件
 */
public abstract class Component {
    //个体和整体都具有的共享
    public void doSomething(){
        //编写业务逻辑
        System.out.println("abstract do something");
    }

    /**
     * 增加一个叶子构件或者树枝构件
     * @param component
     */
    public abstract void add(Component component);

    /**
     * 删除一个叶子构件或者树枝构件
     * @param component
     */
    public abstract void remove(Component component);

    /**
     * 获得分支下所有叶子构件和树枝构件
     * @return
     */
    public abstract List<Component> getChildren();


}

树枝节点:

package com.jack.component;

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

/**
 * Created by jack on 2018/1/26.
 */
public class Composite extends Component {
    //构建容器
    private List<Component> componentList = new ArrayList<>();

    /**
     * 增加一个叶子构件或者树枝构件
     * @param component
     */
    public void add(Component component) {
        this.componentList.add(component);
    }

    /**
     * 删除一个叶子构件或者树枝构件
     * @param component
     */
    public void remove(Component component){
        this.componentList.remove(component);
    }

    /**
     * 获得分支下所有叶子构件和树枝构件
     * @return
     */
    public List<Component> getChildren(){
        return this.componentList;
    }

}
树叶节点:

package com.jack.component;

import java.util.List;

/**
 * Created by jack on 2018/1/26.
 */
public class Leaf extends Component {
    @Override
    public void doSomething() {
        //覆盖写父类的方法
        System.out.println("this is leaf ");
    }

    @Override
    public void add(Component component) throws UnsupportedOperationException{
        //空实现
        throw new UnsupportedOperationException();
    }

    @Override
    public void remove(Component component)throws UnsupportedOperationException {
        //空实现
        throw new UnsupportedOperationException();
    }

    @Override
    public List<Component> getChildren() throws UnsupportedOperationException{
        //空实现
        throw new UnsupportedOperationException();
        //return null;
    }
}


测试代码和上面基本差不多,主要修改了display方法,代码如下:

package com.jack.component;

/**
 * Created by jack on 2018/1/26.
 */
public class Client {
    public static void main(String[] args) {
        //创建一个根节点
        Composite root = new Composite();
        root.doSomething();
        //创建一个树枝构件
        Composite branch = new Composite();
        //创建一个叶子节点
        Leaf leaf = new Leaf();
        root.add(branch);
        branch.add(leaf);
        display(root);
    }

    public static void display(Component root) {
        for (Component component : root.getChildren()) {
            if (component instanceof Leaf) {//叶子节点
                component.doSomething();
            } else {
                //树枝节点
                //display((Composite) component);
                display(component);

            }
        }
    }

}



  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值