zoukankan      html  css  js  c++  java
  • 设计模式之组合模式

    组合模式 Composite

    Intro

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

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

    主要解决:它在我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以像处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

    何时使用:

    1、您想表示对象的部分-整体层次结构(树形结构)。
    2、您希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

    如何解决:树枝和叶子实现统一接口,树枝内部组合该接口。

    关键代码:树枝内部组合该接口,并且含有内部属性 List,里面放 Component。

    使用场景

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

    典型的使用场景:部分、整体场景,如树形菜单,文件、文件夹的管理。

    Sample

    public abstract class Component
    {
        protected string Name;
    
        protected Component(string name)
        {
            Name = name;
        }
    
        public abstract void Add(Component c);
    
        public abstract void Remove(Component c);
    
        public abstract void Display(int depth);
    }
    
    public class Leaf : Component
    {
        public Leaf(string name) : base(name)
        {
        }
    
        public override void Add(Component c)
        {
            throw new System.NotImplementedException();
        }
    
        public override void Remove(Component c)
        {
            throw new System.NotImplementedException();
        }
    
        public override void Display(int depth)
        {
            Console.WriteLine($"{new string('-', depth)} {Name}");
        }
    }
    
    public class Composite : Component
    {
        private readonly List<Component> _children = new List<Component>();
    
        public Composite(string name) : base(name)
        {
        }
    
        public override void Add(Component c)
        {
            _children.Add(c);
        }
    
        public override void Remove(Component c)
        {
            _children.Remove(c);
        }
    
        public override void Display(int depth)
        {
            Console.WriteLine($"{new string('-', depth)} {Name}");
            foreach (var component in _children)
            {
                component.Display(depth + 2);
            }
        }
    }
    
    
    var root = new Composite("root");
    root.Add(new Leaf("Leaf A"));
    root.Add(new Leaf("Leaf B"));
    
    var co = new Composite("CompositeA");
    co.Add(new Leaf("Leaf X"));
    co.Add(new Leaf("Leaf Y"));
    var co1 = new Composite("CompositeA");
    co1.Add(new Leaf("Leaf P"));
    co1.Add(new Leaf("Leaf Q"));
    
    co.Add(co1);
    root.Add(co);
    root.Display(0);
    

    Reference

    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    Putty·Network error:Software caused connection abort
    VSCode·搭建Java开发环境
    MSSQL·将一对多的数据合并为以指定分隔符的数据
    技能Get·Windows10将任何格式文件固定到开始屏幕
    MSSQL·查询TSQL语句执行时间的三种方法
    Javac·编码GBK的不可映射字符
    傅里叶变换、拉氏变换、z变换的含义
    宏、内联函数和普通函数的区别
    OpenGL中创建GLUT菜单
    在PC安裝Android系統+軟體
  • 原文地址:https://www.cnblogs.com/weihanli/p/composite-pattern.html
Copyright © 2011-2022 走看看