zoukankan      html  css  js  c++  java
  • Composite模式学习

    using System;
    using System.Collections;
    namespace Composite
    {
     public interface IBox
     {
      void process();
     }
     public class SingleBox:IBox
     {
      public void process()
      {
      }
     }
     public class ContainerBox:IBox
     {
      public void process()
      {
       // TODO:  添加 ContainerBox.process 实现
      }
      public void Add(IBox box)
      {
       if(list==null)
       {
        list=new ArrayList();
       }
       list.Add(box);
      }
      public void Romove(IBox box)
      {
       if (list==null)
       {
        throw new Exception();
       }
       list.Remove(box);
      }
      public ArrayList getBoxes()
      {
      }
     }

     /// <summary>
     /// //////////////客户代码
     /// </summary>
     class App
     {
      public static void Main()
      {
       IBox=Facory.GetBox();

       //客户代码与对象内部结构发生了耦合
       if(box is ContainerBox)
       {
        box.process();
        ArrayList list=((ContianerBox)box).GetBoxes();
        //...
       }
       else if(box is SingleBox)
       {
        box.process();
       }
      }
     }

    }

    //////////////////////////////////
    ///
    ///基本上比较好判断,不需要重构得出Composite模式,一般应用
    ///于树型结构或明显有容器结构的设计
    namespace Composite2
    {
     public interface IBox
     {
      void process();
      void Add(IBox box);
      void Remve(IBox box);
     }

     /// <summary>
     /// 叶子
     /// </summary>
     public class SingleBox:IBox
     {
      public void process()
      {
       throw UnsuporttedException();
      }
      void Add(IBox box)
      {
       throw UnsuporttedException();
      }
      void Remve(IBox box)
      {
      }
     }
     
     /// <summary>
     /// 树干
     /// </summary>
     public class ContainerBox:IBox
     {
      ArrayList list;
      public void Add(IBox box)
      {
       if(list==null)
       {
        list=new ArrayList();
       }
       list.Add(box);
      }
      public void Remove(IBox box)
      {
       if (list==null)
       {
        throw new Exception();
       }
       list.Remove(box);
      }
      public void process()//递归算法
      {
       //1.do process for myself
       //......

       //2.do process for the box in the list
       if(list!=null)
       {
        foreach(IBox box in list)
        {
         box.process();
        }
       }
      }
     }
     /// <summary>
     /// //////////////客户代码
     /// </summary>
     class App
     {
      public static void Main()
      {
       IBox box =Factory.GetBox();

       //客户对象与抽象接口进行耦合
       box.process();
      }
     }

    }

  • 相关阅读:
    个人冲刺二(7)
    个人冲刺二(6)
    个人冲刺二(5)
    个人冲刺二(4)
    对称二叉树 · symmetric binary tree
    108 Convert Sorted Array to Binary Search Tree数组变成高度平衡的二叉树
    530.Minimum Absolute Difference in BST 二叉搜索树中的最小差的绝对值
    pp 集成工程师 mism师兄问一问
    17. Merge Two Binary Trees 融合二叉树
    270. Closest Binary Search Tree Value 二叉搜索树中,距离目标值最近的节点
  • 原文地址:https://www.cnblogs.com/kuailewangzi1212/p/347021.html
Copyright © 2011-2022 走看看