前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Design Patterns 19 - 组合模式

Design Patterns 19 - 组合模式

作者头像
Reck Zhang
发布2021-08-11 10:59:26
2040
发布2021-08-11 10:59:26
举报
文章被收录于专栏:Reck Zhang

组合模式

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

代码语言:javascript
复制
abstract class Component {
    protected string name;

    public Component(string name) {
        this.name = name;
    }
    public abstract void Add(Component component);
    public abstract void Remove(Component component);
    public abstract void Display(int depth);
}

class Leaf : Component {
    public Leaf(string name) : base(name) {}

    public override void Add(Component component) {
        // cannot add to a leaf
    }

    public override void Remove(Component component) {
        // cannot remove from a leaf
    }

    public override void Display(int depth) {
        // todo
    }
}

class Composite : Component {
    private List<Component> children = new List<Component>();

    public Composite(string name) : base (name) {}

    public override void Add(Component component) {
        children.Add(component);
    }

    public override void Remove(Component component) {
        children.Remove(component);
    }

    public override void Display(int depth) {
        // todo
    }
}

public static void Main(string agrs[]) {
    Composite root = new Composite("root");
    root.Add(new Leaf("Leaf A"));
    root.Add(new Leaf("Leaf B"));

    Composite composite = new Composite("Composite X");
    composite.Add(new Leaf("Leaf XA"));
    composite.Add(new Leaf("Leaf XB"));

    root.Add(composite);
    root.Add(new Leaf("Leaf C"));

    root.Display();
}

透明模式与安全模式

  • 透明模式, 在Component中声明所有用来管理子对象的方法, 其中包括Add, Remove等, 这样实现Component接口的所有子类都具备了Add和Remove. 这样做的好处就是叶节点和枝节点对于外界没有区别, 他们具备完全一致的行为接口. 但问题也很明显, 因为Leaf类本身不具备Add(), Remove()方法的功能, 所以实现他是没有意义的.
  • 安全模式, 也就是在Component接口中不去声明Add和Remove方法, 那么子类的Leaf也就不需要去实现它, 而是在Composite声明所有用来管理子类对象的方法, 这样做就不会出现刚才提到的问题, 不过由于不够透明, 所以树叶和树枝类将不具有相同的接口, 客户端的调用需要做相应的判断, 带来了不便.

何时使用组合模式

需求中是体现部分与整体层次的结构时, 希望用户可以忽略组合对象与单个对象的不同, 统一的使用组合结构中的所有对象时, 就应该考虑用组合模式.

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-02-21,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

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

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 组合模式
    • 透明模式与安全模式
      • 何时使用组合模式
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档