zoukankan      html  css  js  c++  java
  • 设计模式学习笔记组合模式

    概述:                                                                                                      

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

    适用场合:                                                                                                 

    1.你想用部分-整体结构层次。

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

    类图:                                                                                                       

    代码示例:                                                                                                    

    1.定义一个组件接口,无论是叶子和树枝都继承它。

        /// <summary>
    /// 为组合中的对象声明接口,在适当情况下,实现所有类公有接口的的默认行为。
    /// 声明一个接口用于访问和管理Component的子部件
    /// </summary>
    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);
    }

    2.定义叶子,但是这里给出了叶子的无意义的add和reomove方法

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

    /// <summary>
    /// 由于叶子没增加树枝的能力,所以add和remove方法实现它是没有意义的
    /// 但这样可以消除叶节点和枝节点在抽象层次的区别,他们具备完全一致的接口
    /// </summary>
    /// <param name="c"></param>
    public override void Add(Component c)
    {
    Console.WriteLine(
    "Cannot add to a Leaf");
    }
    public override void Remove(Component c)
    {
    Console.WriteLine(
    "Cannot remove from a leaf"); ;
    }
    /// <summary>
    /// 叶节点的具体方法,此处显示其名称和级别
    /// </summary>
    /// <param name="depth"></param>
    public override void Display(int depth)
    {
    Console.WriteLine(
    new String('-',depth)+name);
    }
    }

    3.定义枝节点

        /// <summary>
    /// 定义有行为的枝节点,用来存储子部件,在Componet接口中实现与子部件有关的操作,
    /// 比如add和remove
    /// </summary>
    class Composite:Component
    {
    /// <summary>
    /// 一个子对象集合用来存储其下属的枝节点和叶节点
    /// </summary>
    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);
    }
    /// <summary>
    /// 显示其枝节点名称,并对其下级进行遍历
    /// </summary>
    /// <param name="depth"></param>
    public override void Display(int depth)
    {
    Console.WriteLine(
    new String('-',depth)+name);
    foreach(Component ct in children)
    {
    ct.Display(depth
    +2);
    }
    }
    }

    4,客户端展示小树的生长过程

            /// <summary>
    /// 测试组合模式
    /// </summary>
    static void TestComposite()
    {
    ///生成树根,根上长出两片叶子
    Composite root = new Composite("root");
    root.Add(
    new Leaf("Leaf A"));
    root.Add(
    new Leaf("Leaf B"));

    ///根上长出分支X,分支上又长了两片叶子
    Composite comp = new Composite("Composite X");
    comp.Add(
    new Leaf("Leaf XA"));
    comp.Add(
    new Leaf("Leaf XB"));
    root.Add(comp);

    //分支X上又长出了XY分支,分支上又生了两片嫩叶
    Composite comp2 = new Composite("Composite XY");
    comp2.Add(
    new Leaf("Leaf XYA"));
    comp2.Add(
    new Leaf("Leaf XYB"));
    root.Add(comp2);

    ///根部又长出两片叶子,但是一个叶子D掉落了
    root.Add(new Leaf("Leaf C"));
    Leaf leaf
    = new Leaf("Leaf D");
    root.Add(leaf);
    root.Remove(leaf);

    //从1开始往下遍历,显示整个小树的样子
    root.Display(1);
    Console.Read();


    }

    小结:                                                                                                       

    在System.Web.UI.Control类里面就有Add和Remove方法,也是组合模式的经典应用,但是没有具体查证,希望大家学习的时候能查证一下。

  • 相关阅读:
    HDU 5835 Danganronpa 贪心
    HDU 5842 Lweb and String 水题
    HDU 5832 A water problem 水题
    Codeforces Beta Round #14 (Div. 2) A. Letter 水题
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem K. UTF-8 Decoder 模拟题
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem I. Alien Rectangles 数学
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem F. Turning Grille 暴力
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem C. Cargo Transportation 暴力
    Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem G. k-palindrome dp
  • 原文地址:https://www.cnblogs.com/jqbird/p/2159285.html
Copyright © 2011-2022 走看看