Java设计模式之--装饰模式

来源:互联网 发布:福连成老北京布鞋淘宝 编辑:程序博客网 时间:2024/05/18 13:46

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

     何时用:当系统需要新功能的时候,是向旧的类种添加新的代码,这些新加的代码通常装饰了原有类的核心职责和或主要行为。而这些新加入的东西仅仅是为了满足一些只在某种特定情况下才会执行的特殊行为的需要。而装饰模式提供了很好的解决方案,把每个要装饰的功能放在单独的类中,并让这个类包装它所要装饰的对象。因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了。这样有效的把类的核心职责和装饰功能从类中搬移取出,简化原来的类。


[csharp] view plain copy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.   
  5. namespace 装饰模式  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             Person xc = new Person("小菜");//定义的具体对象ConcreteComponent,为其添加职责,例:Person类,为其搭配服饰  
  12.   
  13.             Console.WriteLine("\n第一种装扮:");  
  14.   
  15.             Sneakers pqx = new Sneakers();  
  16.             BigTrouser kk = new BigTrouser();  
  17.             TShirts dtx = new TShirts();  
  18.   
  19.             //装饰的过程:  
  20.             pqx.Decorate(xc);//先用ConcreteComponent实例化Person类为xc,  
  21.             kk.Decorate(pqx);//按顺序再用Sneaker鞋来搭配BigTrouser垮裤  
  22.             dtx.Decorate(kk);//最后用搭配过的垮裤BigTrouser来搭配TShirtsT恤  
  23.             dtx.Show();//显示第一种   
  24.   
  25.             Console.WriteLine("\n第二种装扮:");  
  26.   
  27.             LeatherShoes px = new LeatherShoes();  
  28.             Tie ld = new Tie();  
  29.             Suit xz = new Suit();  
  30.   
  31.             px.Decorate(xc);  
  32.             ld.Decorate(px);  
  33.             xz.Decorate(ld);  
  34.             xz.Show();  
  35.   
  36.             Console.WriteLine("\n第三种装扮:");  
  37.             Sneakers pqx2 = new Sneakers();  
  38.             LeatherShoes px2 = new LeatherShoes();  
  39.             BigTrouser kk2 = new BigTrouser();  
  40.             Tie ld2 = new Tie();  
  41.   
  42.             pqx2.Decorate(xc);  
  43.             px2.Decorate(pqx);  
  44.             kk2.Decorate(px2);  
  45.             ld2.Decorate(kk2);  
  46.   
  47.             ld2.Show();  
  48.   
  49.             Console.Read();  
  50.         }  
  51.     }  
  52.   
  53.     class Person   //定义的具体对象ConcreteComponent,为其添加职责,例:Person类,为其搭配服饰  
  54.     {  
  55.         public Person()  
  56.         { }  
  57.   
  58.         private string name;  
  59.         public Person(string name)  
  60.         {  
  61.             this.name = name;  
  62.         }  
  63.   
  64.         public virtual void Show()  
  65.         {  
  66.             Console.WriteLine("装扮的{0}", name);  
  67.         }  
  68.     }  
  69.   
  70.     class Finery : Person        //装饰抽象类(Decorator),继承抽象父类,并玩去取代Component  
  71.     {  
  72.         protected Person component;  
  73.   
  74.         //打扮  
  75.         public void Decorate(Person component)  
  76.         {  
  77.             this.component = component;  
  78.         }  
  79.   
  80.         public override void Show()  
  81.         {  
  82.             if (component != null)  
  83.             {  
  84.                 component.Show();  
  85.             }  
  86.         }  
  87.     }  
  88.   
  89.   
  90.     class TShirts : Finery     //具体的装饰对象  
  91.     {  
  92.         public override void Show()  
  93.         {  
  94.             Console.Write("大T恤 ");  
  95.             base.Show();     //调用基类Finery上已被其他方法重写的方法  
  96.         }  
  97.     }  
  98.   
  99.     class BigTrouser : Finery  
  100.     {  
  101.         public override void Show()  
  102.         {  
  103.             Console.Write("垮裤 ");  
  104.             base.Show();  
  105.         }  
  106.     }  
  107.   
  108.     class Sneakers : Finery  
  109.     {  
  110.         public override void Show()  
  111.         {  
  112.             Console.Write("破球鞋 ");  
  113.             base.Show();  
  114.         }  
  115.     }  
  116.   
  117.     class Suit : Finery  
  118.     {  
  119.         public override void Show()  
  120.         {  
  121.             Console.Write("西装 ");  
  122.             base.Show();  
  123.         }  
  124.     }  
  125.   
  126.     class Tie : Finery  
  127.     {  
  128.         public override void Show()  
  129.         {  
  130.             Console.Write("领带 ");  
  131.             base.Show();  
  132.         }  
  133.     }  
  134.   
  135.     class LeatherShoes : Finery  
  136.     {  
  137.         public override void Show()  
  138.         {  
  139.             Console.Write("皮鞋 ");  
  140.             base.Show();  
  141.         }  
  142.     }  
  143. }  

     注意如果只有一个ConcreteComponent类而没有抽象的Component类,那么Decorator类可以是ConcreteComponent的一个子类。同理,如果只有一个ConcreteDecorator类,那么就没必要建立一个单独的Decorator类,而可以把Decorator类和ConcreteDecorator类的责任合并为一个类。
原创粉丝点击