Chinaunix首页 | 论坛 | 博客
  • 博客访问: 629015
  • 博文数量: 87
  • 博客积分: 3399
  • 博客等级: 中校
  • 技术积分: 1422
  • 用 户 组: 普通用户
  • 注册时间: 2010-05-17 21:20
文章分类

全部博文(87)

文章存档

2013年(1)

2012年(51)

2011年(33)

2010年(2)

分类: 项目管理

2011-08-10 20:34:58

装饰模式 Decorator

结构: 意图:

动态的给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

适用性:

l  在不影响其它对象的情况下,以动态、透明的方式给单个对象添加职责。

l  处理那些可以撤销的职责。

l  当不能以生成子类的方法进行扩充时。

n  一种情况是,可能有大量独立的扩展,为支持每一种组合将产生大量的子类,使得子类数目呈爆炸性增长。

n  另一种情况可能是因为类定义被隐藏,或类定义不能用于生成子类。

示例:
  1. // Decorator


  2. // Intent: "Attach additional responsibilites to an object dynamically.

  3. // Decorators provide a flexible alternative to subclassing for

  4. // extending functionality".

  5. //动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式较之于子类提供了一种更加灵活的方式。

  6. // For further information, read "Design Patterns", p175, Gamma et al.,

  7. // Addison-Wesley, ISBN:0-201-63361-2


  8. /* Notes:
  9.  * Dynamically wrap functionality with additonal functionality, so that
  10.  * the client does not know the difference.
  11.  */

  12. namespace Decorator_DesignPattern
  13. {
  14.     using System;

  15.     abstract class Component
  16.     {
  17.         public abstract void Draw();
  18.     }

  19.     class ConcreteComponent : Component
  20.     {
  21.         private string strName;
  22.         public ConcreteComponent(string s)
  23.         {
  24.             strName = s;
  25.         }

  26.         public override void Draw()
  27.         {
  28.             Console.WriteLine("ConcreteComponent - {0}", strName);
  29.         }
  30.     }

  31.     abstract class Decorator : Component
  32.     {
  33.         protected Component ActualComponent;

  34.         public void SetComponent(Component c)
  35.         {
  36.             ActualComponent = c;
  37.         }
  38.         public override void Draw()
  39.         {
  40.             if (ActualComponent != null)
  41.                 ActualComponent.Draw();
  42.         }
  43.     }

  44.     class ConcreteDecorator : Decorator
  45.     {
  46.         private string strDecoratorName;
  47.         public ConcreteDecorator(string str)
  48.         {
  49.             // how decoration occurs is localized inside this decorator

  50.             // For this demo, we simply print a decorator name

  51.             strDecoratorName = str;
  52.         }
  53.         public override void Draw()
  54.         {
  55.             CustomDecoration();
  56.             base.Draw();
  57.         }
  58.         void CustomDecoration()
  59.         {
  60.             Console.WriteLine("In ConcreteDecorator: decoration goes here");
  61.             Console.WriteLine("{0}", strDecoratorName);
  62.         }
  63.     }


  64.     ///

  65.     /// Summary description for Client.

  66.     ///

  67.     public class Client
  68.     {
  69.         Component Setup()
  70.         {
  71.             ConcreteComponent c = new ConcreteComponent("This is the real component");

  72.             ConcreteDecorator d = new ConcreteDecorator("This is a decorator for the component");

  73.             d.SetComponent(c);

  74.             return d;
  75.         }

  76.         public static int Main(string[] args)
  77.         {
  78.             Client client = new Client();
  79.             Component c = client.Setup();

  80.             // The code below will work equally well with the real component,

  81.             // or a decorator for the component


  82.             c.Draw();
  83.             Console.Read();
  84.             return 0;
  85.         }
  86.     }
  87. }
阅读(863) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~