zoukankan      html  css  js  c++  java
  • 装饰模式(Decorator Pattern)

    子类复子类,子类何其多
        假如我们需要为游戏中开发一种坦克,除了各种不同型号的坦克外,我们还希望在不同场合中为其增加以下一种或多种功能;比如红外线夜视功能,比如水陆两栖功能,比如卫星定位功能等等。
    按类继承的作法如下:
      
    1  //抽象坦克
    2  public abstract class Tank
    3     {
    4        public abstract void Shot();
    5        public abstract void Run();
    6     }
    各种型号:
     1 //T50型号
     2    public class T50:Tank
     3     {
     4         public override void Shot()
     5         {
     6             Console.WriteLine("T50坦克平均每秒射击5发子弹");
     7         }
     8         public override void Run()
     9         {
    10             Console.WriteLine("T50坦克平均每时运行30公里");
    11         }
    12     }

     1 //T75型号 
     2  public class T75 : Tank
     3     {
     4         public override void Shot()
     5         {
     6             Console.WriteLine("T75坦克平均每秒射击10发子弹");
     7         }
     8         public override void Run()
     9         {
    10             Console.WriteLine("T75坦克平均每时运行35公里");
    11         }
    12     }

     1 //T90型号  
     2  public class T90 :Tank
     3     {
     4         public override void Shot()
     5         {
     6             Console.WriteLine("T90坦克平均每秒射击10发子弹");
     7         }
     8         public override void Run()
     9         {
    10             Console.WriteLine("T90坦克平均每时运行40公里");
    11         }
    12     }
    各种不同功能的组合:比如IA具有红外功能接口、IB具有水陆两栖功能接口、IC具有卫星定位功能接口。

     1 //T50坦克各种功能的组合
     2 public class T50A:T50,IA
     3 {
     4   //具有红外功能
     5 }
     6 public class T50B:T50,IB
     7 {
     8  //具有水陆两栖功能
     9 }
    10 public class T50C:T50,IC
    11 {
    12
    13 }
    14 public class T50AB:T50,IA,IB
    15 {}
    18 public class T50AC:T50,IA,IC
    19 {}
    20 public class T50BC:T50,IB,IC
    21 {}
    22 public class T50ABC:T50,IA,IB,IC
    23 {}

     1 
     2   //T75各种不同型号坦克各种功能的组合
     3   public class T75A:T75,IA
     4   {
     5     //具有红外功能
     6   }
     7   public class T75B:T75,IB
     8   {
     9    //具有水陆两栖功能
    10   }
    11  public class T75C:T75,IC
    12  {
    13    //具有卫星定位功能
    14  }
    15  public class T75AB:T75,IA,IB
    16  {
    17   //具有红外、水陆两栖功能
    18 }
    19  public class T75AC:T75,IA,IC
    20  {
    21   //具有红外、卫星定位功能
    22 }
    23  public class T75BC:T75,IB,IC
    24  {
    25  //具有水陆两栖、卫星定位功能
    26 }
    27  public class T75ABC:T75,IA,IB,IC
    28  {
    29   //具有红外、水陆两栖、卫星定位功能
    30 }

     1 
     2   //T90各种不同型号坦克各种功能的组合
     3   public class T90A:T90,IA
     4   {
     5     //具有红外功能
     6   }
     7   public class T90B:T90,IB
     8   {
     9    //具有水陆两栖功能
    10   }
    11  public class T90C:T90,IC
    12  {
    13    //具有卫星定位功能
    14  }
    15  public class T90AB:T90,IA,IB
    16  {
    17   //具有红外、水陆两栖功能
    18 }
    19  public class T90AC:T90,IA,IC
    20  {
    21   //具有红外、卫星定位功能
    22 }
    23  public class T90BC:T90,IB,IC
    24  {
    25  //具有水陆两栖、卫星定位功能
    26 }
    27  public class T90ABC:T90,IA,IB,IC
    28  {
    29   //具有红外、水陆两栖、卫星定位功能
    30 }
    由此可见,如果用类继承实现,子类会爆炸式地增长。
    动机(Motivate):
        上述描述的问题根源在于我们“过度地使用了继承来扩展对象的功能”,由于继承为类型引入的静态物质,使得这种扩展方式缺乏灵活性;并且随着子类的增多(扩展功能的增多),各种子类的组合(扩展功能组合)会导致更多子类的膨胀(多继承)。
       如何使“对象功能的扩展”能够根据需要来动态地实现?同时避免“扩展功能的增多”带来的子类膨胀问题?从而使得任何“功能扩展变化”所导致的影响将为最低?
    意图(Intent):
        动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。
                                                                  ------《设计模式》GOF
    结构图(Struct):
                    



    生活中的例子:
                              

    适用性:
        需要扩展一个类的功能,或给一个类增加附加责任。

        需要动态地给一个对象增加功能,这些功能可以再动态地撤销。

        需要增加由一些基本功能的排列组合而产生的非常大量的功能,从而使继承关系变得不现实。

    实现代码:
       
    1 namespace Decorator
    2 {
    3   public abstract class Tank
    4     {
    5        public abstract void Shot();
    6        public abstract void Run();
    7     }
    8 }

     1 namespace Decorator
     2 {
     3    public class T50:Tank
     4     {
     5         public override void Shot()
     6         {
     7             Console.WriteLine("T50坦克平均每秒射击5发子弹");
     8         }
     9         public override void Run()
    10         {
    11             Console.WriteLine("T50坦克平均每时运行30公里");
    12         }
    13     }
    14 }

     1 namespace Decorator
     2 {
     3     public class T75 : Tank
     4     {
     5         public override void Shot()
     6         {
     7             Console.WriteLine("T75坦克平均每秒射击10发子弹");
     8         }
     9         public override void Run()
    10         {
    11             Console.WriteLine("T75坦克平均每时运行35公里");
    12         }
    13     }
    14 }

     1 namespace Decorator
     2 {
     3     public class T90 :Tank
     4     {
     5         public override void Shot()
     6         {
     7             Console.WriteLine("T90坦克平均每秒射击10发子弹");
     8         }
     9         public override void Run()
    10         {
    11             Console.WriteLine("T90坦克平均每时运行40公里");
    12         }
    13     }
    14 }

     1 namespace Decorator
     2 {
     3    public abstract class Decorator :Tank //Do As 接口继承 非实现继承
     4     {
     5        private Tank tank; //Has a  对象组合
     6        public Decorator(Tank tank)
     7        {
     8            this.tank = tank;
     9        }
    10        public override void Shot()
    11        {
    12            tank.Shot();
    13        }
    14        public override void Run()
    15        {
    16            tank.Run();
    17        }
    18     }
    19 }
    20 

     1 
     2 namespace Decorator
     3 {
     4    public class DecoratorA :Decorator
     5     {
     6        public DecoratorA(Tank tank) : base(tank)
     7        {
     8        }
     9       public override void Shot()
    10       {
    11          //Do some extension //功能扩展 且有红外功能
    12           base.Shot();
    13       }
    14     public override void Run()
    15     {
    16 
    17         base.Run();
    18     }
    19     }
    20 }

     1 namespace Decorator
     2 {
     3     public class DecoratorB :Decorator
     4     {
     5       public DecoratorB(Tank tank) : base(tank)
     6        {
     7        }
     8       public override void Shot()
     9       {
    10          //Do some extension //功能扩展 且有水陆两栖功能
    11          base.Shot();
    12       }
    13     public override void Run()
    14     {
    15 
    16         base.Run();
    17     }
    18    }    
    19 }
    20 

     1 namespace Decorator
     2 {
     3     public class DecoratorC :Decorator
     4     {
     5        public DecoratorC(Tank tank) : base(tank)
     6        {
     7        }
     8       public override void Shot()
     9       {
    10          //Do some extension //功能扩展 且有卫星定位功能
    11          base.Shot();
    12       }
    13     public override void Run()
    14     {
    15 
    16         base.Run();
    17     } 
    18         
    19    }
    20 }

     1    class Program
     2     {
     3         static void Main(string[] args)
     4         {
     5             Tank tank = new T50();
     6             DecoratorA da = new DecoratorA(tank); //且有红外功能
     7             DecoratorB db = new DecoratorB(da);   //且有红外和水陆两栖功能
     8             DecoratorC dc = new DecoratorC(db);   //且有红外、水陆两栖、卫星定们三种功能
     9             dc.Shot();
    10             dc.Run();
    11         }
    12     }

    Decorator模式的几个要点:
        通过采用组合、而非继承的手法,Decorator模式实现了在运行时动态地扩展对象功能的能力,而且可以
    根据需要扩展多个功能。避免了单独使用继承带来的“灵活性差"和"多子类衍生问题"。
        Component类在Decorator模式中充当抽象接口的角色,不应该去实现具体的行为。而且Decorator类对于Component类应该透明---换言之Component类无需知道Decorator类,Decorator类是从外部来扩展Component类的功能。
        Decorator类在接口上表现为is-a Component的继承关系,即Decorator类继承了Component类所且有的接口。但在实现上又表现has a Component的组合关系,即Decorator类又使用了另外一个Component类。我们可以使用一个或者多个Decorator对象来“装饰”一个Component对象,且装饰后的对象仍然是一个Component对象。
        Decorator模式并非解决”多子类衍生的多继承“问题,Decorator模式应用的要点在于解决“主体
    类在多个方向上的扩展功能”------是为“装饰”的含义。

    Decorator在.NET(Stream)中的应用:
        

    可以看到, BufferedStreamCryptoStream其实就是两个包装类,这里的Decorator模式省略了抽象装饰角色(Decorator),示例代码如下:
     1 class Program
     2 
     3 {
     4 
     5     public static void Main(string[] args)
     6 
     7     {
     8 
     9         MemoryStream ms =
    10 
    11             new MemoryStream(new byte[] { 100,456,864,222,567});
    12 
    13  
    14 
    15         //扩展了缓冲的功能
    16 
    17         BufferedStream buff = new BufferedStream(ms);
    18 
    19  
    20 
    21         //扩展了缓冲,加密的功能
    22 
    23         CryptoStream crypto = new CryptoStream(buff);
    24 
    25     }
    26 
    27 }

    通过反编译,可以看到BufferedStream类的代码(只列出部分),它是继承于Stream类:
     1 public sealed class BufferedStream : Stream
     2 
     3 {
     4 
     5     // Methods
     6 
     7     private BufferedStream();
     8 
     9     public BufferedStream(Stream stream);
    10 
    11     public BufferedStream(Stream stream, int bufferSize);
    12 
    13     // Fields
    14 
    15     private int _bufferSize;
    16 
    17     private Stream _s;
    18 
    19 }


  • 相关阅读:
    block: cfq 学习01
    SAS,SATA普及文档
    如何查看盘的类型
    [转载] Linux Futex的设计与实现
    C++ std::map的安全遍历并删除元素的方法
    我的vimrc配置
    .vimrc
    RC: blkio throttle 测试
    LTTng
    基于ADL5317的雪崩光电二极管(APD)偏压控制/光功率监测电路的设计
  • 原文地址:https://www.cnblogs.com/abcdwxc/p/884495.html
Copyright © 2011-2022 走看看