组合模式

概述组合模式有时候又叫做部分-整体模式,它使我们树型结构的问题中,模糊了简单元素和复杂元素的概念,客户程序可以向处理简单元素一样来处理复杂元素,从而使得客户程序与复杂元素的内部结构解耦。

意图将对象组合成树形结构以表示“部分-整体”的层次结构。Composite模式使得用户对单个对象和组合对象的使用具有一致性。[GOF 《设计模式》]

一般的我们认为,客户代码与抽象接口耦合是最好的,是最好的方式。我下面将对这句话进行阐述,包括对部分与整体的阐述。困,明天继续。上班太累。

UML:

背景:现在是有箱子的操作,复合箱子里面还可以有箱子。我们先看下代码。

   public interface IBox//Component
   {
        void Process();
   }

那么接下来有两种类型的box,一种是singlebox,另一种是containerbox。都继承Ibox。

    public class SingleBox:IBox
    {
        //每个box都有一个操作?如开箱操作吧
        public void Process() 
        {
            Console.WriteLine("Do Something!");
        } 
    }
    //这个ContainerBox虽然也是一个box,但是它却不是一个简单的singlebox
    //它还包含有其他box
    public class ContainerBox:IBox
    {
       
        public void Process() {
            Console.WriteLine("Do Something!");
        }

        public ArrayList GetBoxs() {
            ArrayList list = new ArrayList();
            //...
            return list;
        }
    }

那么客户程序是怎么调用呢?

       static void Main(string[] args)
        {
            ContainerBox containerBox = new ContainerBox();
            containerBox.Process();
            foreach (ContainerBox item in containerBox.GetBoxs())
            {
                //...
            }
        }

客户程序就要判断你是不是一个复合类型的,如果是复合类型的我还要遍历然后才能处理。single的就直接调用Process方法处理。这样就增加了客户程序对ContainerBox的类的耦合。那么我们该如何做才能解耦呢?客户直接调用抽象是最好的方法。那么现在Composite模式就派上用场了。接下来看看改进后的代码。

   public class ContainerBox:IBox
    {
        ArrayList list = null;

        public void Process() {
            foreach (ContainerBox item in list)
            {
                item.Process();
            }
            Console.WriteLine("Do Something!");
        }
        //为了方便对子对象的管理 必须提供Add Remove方法
        public void Add(IBox box) {
            if (list == null) {
                list = new ArrayList();
            }
            list.Add(box);
        }

        public void Remove(IBox box) {
            if (list == null) {
                list = new ArrayList();
            }
            list.Remove(box);
        }

        
    }

那么现在ContainerBox和IBox就是继承和组合的关系了。在Add中有用到IBox故也是组合的关系。现在客户端调用就直接调用接口了:

      static void Main(string[] args)
        {
            SingleBox singleBox = new SingleBox();
            ContainerBox containerBox = new ContainerBox();

            containerBox.Add(singleBox);
            //遍历就直接再ContainerBox里面实现
            containerBox.Process();
           
        }

其实,组合的模式在到现在还没有真正在项目中用过,现在学了还是感觉有的地方没那么理解。希望在日后的项目中会用到,累了,想早点休息,晚安吧。

原文地址:https://www.cnblogs.com/huaizuo/p/2617574.html