zoukankan      html  css  js  c++  java
  • Composite 组合(结构型模式)

    image

    对象容器的问题
    在面向对象系统中,我们常会遇到一类具有“容器”特征的对象——即它们在充当对象的同时,又是其他对象的容器。

    public class SingleBox: IBox {
    public void process() { ……}
    }
    public class ContainerBox :IBox {
    public void process(){……}
    public ArrayList getBoxes(){……}
    }


    如果我们要对这样的对象容器进行处理:

    IBox box=Factory.GetBox();
    If (box 
    is ContainerBox){
    box.process();
    ArrayList list
    = ((ContrainerBox) box).GetBoxes();
    …….. 
    // 将面临比较复杂的递归处理
    }else if( box is SingleBox){
    box.process();

      

    动机(Motivation)
          上述描述的问题根源在于:客户代码过多地依赖于对象容器复杂的内部实现结构,对象容器内部实现结构(而非抽象接口)的变化将引起客户代码的频繁变化,带来了代码的维护性、扩展性等弊端。
          如何将“客户代码与复杂的对象容器结构”解耦?让对象容器自己来实现自身的复杂结构,从而使得客户代码就像处理简单对象一样来处理复杂的对象容器?

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

    结构(Structure)

     

    image

    未使用组合模式

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Collections;

    namespace testWpf
    {
        
    //未使用组合模式
        
    //asp.net控件也是使用了组合模式,button和pannel控件也有一个属性Controls,但button不实现controls方法

        
    public interface IBox
        {
            
    void process();
        }

        
    public class SingleBox : IBox 
        {
            
    public void process() { }
        }

        
    public class ContainerBox : IBox
        {
            
    public void add(IBox box) { }
            
    public void Remove(IBox box) { }
            
    public void process() { }
            
    public ArrayList getBoexes() { return new ArrayList(); }    
        }

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

                
    //客户代码于对象内部结构发生了耦合
                if (box is ContainerBox)
                {
                    box.process();
                    ArrayList list 
    = ((ContainerBox)box).getBoexes();
                    
    //... 将面临比较复杂的递归处理
                }
                
    else if (box is SingleBox)
                {
                    box.process();
                }
            }
        }


    }

     

    使用了组合模式

    代码
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    using System.Collections;

    namespace testWpf
    {
        
    //使用了组合模式

        
    public interface IBox
        {
            
    void process();
            
    void Add(IBox box);
            
    void Remove(IBox box);
        }

        
    public class SingleBox : IBox
        {
            
    public void process() 
            {
                
    //1.Do Something for myself
                
    //... ...
            }

            
    //不是容器对象,不支持的方法可以选择抛出异常
            public void Add(IBox box)
            {
                
    throw new Exception("not supported");
            }
            
    public void Remove(IBox box)
            {
                
    throw new Exception("not supported");
            }
        }

        
    public class ContainerBox : IBox
        {
            ArrayList list 
    = null;

            
    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("error");
                }
                list.Remove(box);
            }

            
    public void process() 
            {
                
    //1.Do Something 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 
    = Factor.GetBox();

                
    //客户代码与抽象接口进行耦合,这是面向对象设计的导向
                box.process();
                
            }
        }
    }

      

    Composite模式的几个要点

    1. Composite模式采用树形结构来实现普遍存在的对象容器,从而将“一对多”的关系转化为“一对一”的关系,使得客户代码可以一致地处理对象和对象容器,无需关心处理的是单个的对象,还是组合的对象容器。
    2. 将“客户代码与复杂的对象容器结构”解耦是Composite模式的核心思想,解耦之后,客户代码将与纯粹的抽象接口——而非对象容器的复内部实现结构——发生依赖关系,从而更能“应对变化”。
    3. Composite模式中,是将“Add和Remove等和对象容器相关的方法”定义在“表示抽象对象的Component类”中,还是将其定义在“表示对象容器的Composite类”中,是一个关乎“透明性”和“安全性”的两难问题,需要仔细权衡。这里有可能违背面向对象的“单一职责原则”,但是对于这种特殊结构,这又是必须付出的代价。ASP.NET控件的实现在这方面为我们提供了一个很好的示范。
    4. Composite模式在具体实现中,可以让父对象中的子对象反向追溯;如果父对象有频繁的遍历需求,可使用缓存技巧来改善效率。
  • 相关阅读:
    Typescript 学习笔记一:介绍、安装、编译
    css 如何“画”一个抽奖转盘
    isBalanced函数实现
    链表(单向链表,双向链表)
    IDEA 插件的安装和使用
    leetCode算法------>>>>数组
    二维数组
    IDEA (2019.3) 字体编码和基本设置
    线性结构和非线性结构
    Java反射
  • 原文地址:https://www.cnblogs.com/timy/p/1626772.html
Copyright © 2011-2022 走看看