zoukankan      html  css  js  c++  java
  • 设计模式のCompositePattern(组合模式)----结构模式

    一、产生背景

    又叫部分整体模式,是用于把一组相似的对象当作一个单一的对象。组合模式依据树形结构来组合对象,用来表示部分以及整体层次。这种类型的设计模式属于结构型模式,它创建了对象组的树形结构。

    这种模式创建了一个包含自己对象组的类。该类提供了修改相同对象组的方式。

    我们通过下面的实例来演示组合模式的用法。实例演示了一个组织中员工的层次结构。

    二、通常做法

    组合模式参与者:

      ◊ Component

        ° 声明组合中对象的接口;

        ° 实现全部类中公共接口的默认行为;

        ° 声明访问和管理子类的接口;

        ° (可选择)定义接口提供在递归结构中访问父类。

      ◊ Leaf

        ° 表示在组合对象中叶子节点对象,没有子节点;

        ° 定义组合对象中的初始行为。

      ◊ Composite

        ° 定义Component子类的行为;

        ° 保存Component子类;

        ° 实现Component接口的子类关联操作。

      ◊ Client

        ° 通过Component接口组合多个对象。

    三、实例

    1、component.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CompositionPattern
    {
        public abstract class Component
        {
            protected string _name;
    
            public Component(string name)
            {
                _name = name;
            }
    
            public abstract void Add(Component c);
    
            public abstract void Remove(Component c);
    
            public abstract void Display(int depth);
    
        }
    }

    2、composite.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CompositionPattern
    {
        public 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)
            {
                Console.WriteLine(new String('-', depth) + _name);
    
                foreach (Component component in _children)
                {
                    component.Display(depth + 2);
                }
            }
        }
    }

    3、leaf.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CompositionPattern
    {
        public  class Leaf:Component
        {
            public Leaf(string name) : base(name) { }
    
            public override void Add(Component c)
            {
                Console.WriteLine("Cannot add to a");
            }
    
            public override void Remove(Component c)
            {
                Console.WriteLine("Cannot remove from a leaf");
            }
    
            public override void Display(int depth)
            {
                Console.WriteLine(new String('-', depth) + _name);
            }
        }
    }

    4、客户端

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace CompositionPattern
    {
        class Program
        {
            static void Main(string[] args)
            {
                // Create a tree structure
                Composite root = new Composite("root");
                root.Add(new Leaf("Leaf A"));
                root.Add(new Leaf("Leaf B"));
    
                Composite comp = new Composite("Composite X");
                comp.Add(new Leaf("Leaf XA"));
                comp.Add(new Leaf("Leaf XB"));
    
                root.Add(comp);
                root.Add(new Leaf("Leaf C"));
    
                // Add and remove a leaf
                Leaf leaf = new Leaf("Leaf D");
                root.Add(leaf);
                root.Remove(leaf);
    
                // Recursively display tree
                root.Display(1);
    
                Console.ReadLine();
    
            }
        }
    }

    四、设计模式分析

      组合模式可以适用以下情形:

      ◊ 希望把对象表示成部分—整体层次结构;

      ◊ 希望用户忽略组合对象与单个对象的不同,用户将统一地使用组合结构中所有对象。

      组合模式具有以下特点:

      ◊ 定义了包含基本对象和组合对象的类层次结构。基本对象可以被组合成更复杂的组合对象,而这个组合对象又可以被组合,不断的递归下去。客户代码中,任何用到基本对象的地方都可以使用组合对象;

      ◊ 简化客户代码。客户可以一致地使用组合结构和单个对象。这样用户就不必关心处理的是一个叶子节点还是一个组合组件,从而简化了客户代码;

      ◊ 使得新增类型的组件更加容易。新定义的Composite或Leaf子类自动地与已有的结构和客户代码一起协同工作,客户程序不需因新的Component类而改变。

  • 相关阅读:
    如何利用azMan (Authorization Manager) 实现 rolebased的安全验证机制
    如何处理源dump文件的mscordacwks.dll文件与调试机上的版本不一致问题而无法使用extension cmd的问题
    UI thread client callback和UI thread WCF Service一起工作时死锁的形成原因及解决方法
    如何在 IIS 6.0 上配置托管的 Web 应用程序时使用 SPN(包括Network service ,domain acount, NLB, host header等各种情况)
    WCF中各种Transaction的分类及其相关参数的设置
    利用windbg调试class type,value type以及MethodTable等强化C#的基本概念
    如何自定义Attribute class并将其应用到相应的class
    谷歌浏览器F12调试页面学习
    上班族致富之道
    折半插入排序
  • 原文地址:https://www.cnblogs.com/xietianjiao/p/8509638.html
Copyright © 2011-2022 走看看