前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >设计模式(六)——组合模式

设计模式(六)——组合模式

作者头像
小森啦啦啦
发布2019-07-13 13:35:04
3470
发布2019-07-13 13:35:04
举报
文章被收录于专栏:IT笔记分享IT笔记分享

组合模式

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

一致性指的是这些对象都具有相同的接口。

优点:

  • 高层模块调用简单
  • 节点自有增加,容易扩展。

缺点:

直接调用实现类,不符合依赖倒置原则。

使用场景

  • 维护和展示部分整体关系场景,如菜单、文件和文件夹管理等。
  • 从一个整体中能够独立出部分模块或功能的场景。

透明方式

父类包含所有子类方法,父类方法是抽象的。不需要该方法的子类可以选择throw Exception。

优点: 各个子类完全一致,可以使用父类接口调用。

缺点: 子类实现无意义方法。

安全方式

子类特有的方法,留到具体子类中实现。树枝节点和树叶节点彻底分开。

缺点: 所有子类不再具有相同接口。

代码实现

GitHub

例子是透明方式实现的组合模式

Compent 抽象构件,定义参加组合对象的共有方法和属性。

代码语言:javascript
复制
public abstract class Component {    protected String name;    public Component(String name){        this.name = name;    }    abstract void add(Component component);    abstract void delete(Component component);    abstract void show(int index);}

Composite树枝构件,组合树枝节点和树叶节点形成一个树形结构。

代码语言:javascript
复制
public class Composite extends Component {    List<Component> components = new ArrayList<>();    public Composite(String name){        super(name);    }    @Override    void add(Component component) {        components.add(component);    }    @Override    void delete(Component component) {        components.remove(component);    }    @Override    void show(int index) {        StringBuilder stringBuilder = new StringBuilder();        for(int i = 0; i < index; i++){            stringBuilder.append("-");        }        System.out.println(stringBuilder.toString() + this.name);        for(Component component : components){            component.show(index + 2);        }    }}

Leaf树叶节点,其下没有其他分支

代码语言:javascript
复制
public class Leaf extends Component {    public Leaf(String name){        super(name);    }    @Override    void add(Component component) {        System.out.println("树叶节点没有此功能");    }    @Override    void delete(Component component) {        System.out.println("树叶节点没有此功能");    }    @Override    void show(int index) {        StringBuilder stringBuilder = new StringBuilder();        for(int i = 0; i < index; i++){            stringBuilder.append("-");        }        System.out.println(stringBuilder.toString() + this.name);    }}

测试类

代码语言:javascript
复制
public static void main(String[] args){    Composite root = new Composite("root");    root.add(new Leaf("leaf1"));    Leaf leaf = new Leaf("leaf2");    root.add(leaf);    Composite composite = new Composite("composite");    composite.add(new Leaf("leaf3"));    composite.add(new Leaf("leaf4"));    composite.add(new Leaf("leaf5"));    root.add(composite);    root.show(1);    System.out.println("===================");    root.delete(leaf);    root.show(1);    System.out.println("===================");    composite.show(1);}

返回信息

代码语言:javascript
复制
-root---leaf1---leaf2---composite-----leaf3-----leaf4-----leaf5===================-root---leaf1---composite-----leaf3-----leaf4-----leaf5===================-composite---leaf3---leaf4---leaf5

本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2019-06-13,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 IT笔记分享 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 组合模式
    • 优点:
      • 缺点:
        • 使用场景
          • 透明方式
            • 安全方式
              • 代码实现
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com