• 23设计模式之 组合模式


    组合模式(composite),将对象组合成树形结构,可以用来描述整体与部分的关系,组合模式把部分与整体的关系用树结构表示出来。组合模式使得用户对单个对象和组合对象的使用具有一致性!

    涉及角色:

    1.Component 是组合中的对象声明接口,在适当的情况下,实现所有类共有接口的默认行为。声明一个接口用于访问和管理Component子部件。
    2.Leaf 在组合中表示叶子结点对象,叶子结点没有子结点。
    3.Composite 定义有枝节点行为,用来存储子部件,在Component接口中实现与子部件有关操作,如增加(add)和删除(remove)等。

    适用性

    以下情况下适用Composite模式:
    1.你想表示对象的部分-整体层次结构
    2.你希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中的所有对象。

    基类,抽象类或接口

    View Code
     public abstract class Component
        {
            protected string name;
    
            public Component(string name)
            {
                this.name = name;
            }
    
            public abstract void Add(Component c);
            public abstract void Remove(Component c);
            public abstract void Display(int depth);
        }

    具体节点类 实现接口 树枝节点

    View Code
     public class Leaf : Component
        {
            public Leaf(string name):base(name)
            {
     
            }
    
            public override void Add(Component c)
            {
                Console.WriteLine("不能再增加节点了");
            }
    
            public override void Remove(Component c)
            {
                Console.WriteLine("不能再移除节点了");
            }
    
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + name);
            }
        }

    定义树枝节点的行为,存储子部件

    View Code
     class Composite : Component
        {
            private 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 (Component comp in children)
                {
                    comp.Display(depth + 2);
                }
            }
        }

    客户端代码调用

    View Code
    static void Main(string[] args)
            {
                Composite root = new Composite("root");
                root.Add(new Leaf("leaf A"));
                root.Add(new Leaf("left B"));
    
                Composite comp = new Composite("Composite X");
                comp.Add(new Leaf("Left XA"));
                comp.Add(new Leaf("Left XB"));
    
                root.Add(comp);
    
                Composite comp2 = new Composite("Composite XY");
                comp2.Add(new Leaf("Leaf XYA"));
                comp2.Add(new Leaf("Leaf XBY"));
                comp.Add(comp2);
    
                root.Add(new Leaf("Leaf C"));
    
                Leaf leaf = new Leaf("Leaf D");
                root.Add(leaf);
                root.Remove(leaf);
    
                root.Display(1);
                Console.Read();
            }

    执行结果

    -root
    ---leaf A
    ---left B
    ---Composite X
    -----Left XA
    -----Left XB
    -----Composite XY
    -------Leaf XYA
    -------Leaf XBY
    ---Leaf C

  • 相关阅读:
    A. Two Semiknights Meet DFS
    Dummy Guy 几何
    Electrification Plan MST
    Parallel and Perpendicular 几何,数学
    A. Soroban 暴力+水题
    B. Fence 前缀和 水题
    Max Sum http://acm.hdu.edu.cn/showproblem.php?pid=1003
    亲和串 http://acm.hdu.edu.cn/showproblem.php?pid=2203
    N! http://acm.hdu.edu.cn/showproblem.php?pid=1042
    Let the Balloon Rise http://acm.hdu.edu.cn/showproblem.php?pid=1004
  • 原文地址:https://www.cnblogs.com/binyao/p/3041289.html
Copyright © 2020-2023  润新知