当前位置: 移动技术网 > IT编程>软件设计>设计模式 > 装饰模式(Decorator Pattern)

装饰模式(Decorator Pattern)

2019年05月12日  | 移动技术网IT编程  | 我要评论
  • 装饰模式概述

定义:动态地给一个对象增加一些附属的职责。

装饰装饰,自然的理解就是在原有物品的基础上,增加一些别的东西,让它变得更令人满意。且装饰模式是在不改变对象本身的基础上就行额外的增加,更加灵活

比如买房,首先买的是个空房,随后我们会放进去家具,和各种生活中要用的东西,让这个家变得更有家的味道,这就是装饰。我们也没有改变房子的结构。下次,也可以重新对家具进行布局,变成另外的风格

  • 装饰模式的结构和实现

component(抽象构件): (空房):具体构件和抽象装饰类的父类,声明了具体构件中的业务方法

1 abstract class component
2 {
3     public abstract void operation();
4 }

concretecomponent(具体构件类):(刚买的北京二环内的一套房)抽象构件类的子类,用于定义具体的构件对象,实现在抽象构件中声明的方法。装饰类可以为它增加额外职责

1 class concretecomponent : component
2 {
3     public override void operation()
4     {
5         //功能实现
6     }

decorator(抽象装饰类):(家具城)抽象构件类的子类,用来给具体构件增加职责

 1 class decorator :component
 2 {
 3     private component component;//抽象构件对象的引用
 4     
 5     public decorator(component component)
 6     {
 7         this.component = component;
 8     }
 9     
10     public override void operation()
11     {
12         component.operation();//调用原有业务方法
13     }
14 }

concretedecorator(具体装饰类):(桌子 椅子 电器 床.......)抽象装饰类的子类,负责向构件增加新的职责

 1 class concretedecorator :decorator
 2 {
 3     public concretedecorator(component component ):base (component)
 4     {
 5     }
 6     
 7     public void addedbehavir()
 8     {
 9         //新的业务方法
10     }
11     
12     public override void operation()
13     {
14         base.operation();//调用原有方法
15         addedbehavir();//调用新的方法
16     }
17 }
  • 装饰模式的应用

 现有一家咖啡店,可以根据客户需求提供对应的咖啡。咖啡{  {浓缩咖啡(espresso),25},{混合咖啡(house blend),30},{重烘焙咖啡(dark roast),20}  }

配料{ {摩卡(mocha),10} ,{奶泡(whip),8 },{牛奶 (milk),6} }

输出 浓缩咖啡,摩卡,牛奶  41

分析:这就是个混合搭配的情况,只要你有钱,一种咖啡想搭配多少种配料都可以,那我们算算,3种咖啡,3种配料,一共可以搭配出多少种不同的饮料呢???不难算出,一种咖啡可以有8种(2^3)搭配,那么3种咖啡共有24种。那么这题写出24种类不就行了。当然是可以的。用继承,也是可以的。但是,由于自由搭配的情况,导致把全部情况写出来是完全低效的。所以,饮料类可以作为抽象类,配料类作为装饰类,使用装饰模式。我们想想,咖啡和配料的搭配,无论是咖啡搭配配料,还是配料搭配咖啡,效果都是一样的。但是,为了不影响咖啡类本身,我们是将配料类关联咖啡类的,即将咖啡类的对象作为自己的成员变量。

component(抽象构件):coffee

1 abstract  class coffee//饮料类
2 {    
3          public abstract  double cost();
4          public abstract string description();
5 }

concretecomponent(具体构件类):浓缩咖啡(espresso)、混合咖啡(house blend)、重烘焙咖啡(dark roast)

 1 class espresso : coffee //特浓咖啡(espresso) vs 滴漏咖啡(drip)  特浓咖啡的制作原理是让热水在高压下流过一把压得严严实实的细腻的咖啡粉,咖啡因浓度较高,有咖啡脂。
 2     {
 3         public override double cost()
 4         {
 5             return 25;
 6         }
 7 
 8         public override string description()
 9         {
10             return "浓缩咖啡";
11         }
12     }
13 class darkroast : coffee //深培咖啡:按照美式分级,咖啡豆的烘焙由浅至深可分为八个阶段。咖啡的味道主要取决于烘焙度,一般烘焙程度越深苦味越浓,烘焙程度越低,酸味就越高。
14     {
15         public override double cost()
16         {
17             return 20;
18         }
19 
20         public override string description()
21         {
22             return "深培咖啡";
23         }
24     }
25  class houseblend : coffee //北美特别流行的黑咖啡,它属于混合咖啡,配方因地而有所不同,但主要成分都有50%以上的哥伦比亚咖啡豆.可以翻译为家常咖啡。
26     {    
27         public override double cost()
28         {
29             return 30;
30         }
31         public override string description()
32         {
33             return "混合咖啡";
34         }
35     }

decorator(抽象装饰类):ingredients

 1 abstract class ingredients:coffee 
 2     {
 3         private coffee coffee;
 4 
 5         public ingredients(coffee beverage)
 6         {
 7             this.coffee = beverage;
 8         }
 9         public override double cost()
10         {
11             return coffee.cost();
12         }
13         public override string description()
14         {
15             return coffee.description();
16         }  
17     }

concretedecorator(具体装饰类):mocha、whip、milk

 1 class mocha:ingredients  
 2     {
 3         public mocha(coffee beverage):base(beverage )
 4         {
 5         }
 6   
 7         public override double cost()
 8         {
 9             return base.cost() + 10;
10         }
11 
12         public override string description()
13         {
14             return base.description()+",摩卡";
15         }
16     }
17 
18     class whip : ingredients //奶泡,由全脂牛奶搅拌而成,可拉丝
19     {
20         public whip(coffee beverage)
21             : base(beverage)
22         {   
23         }
24 
25         public override double cost()
26         {
27             return base.cost() + 8;
28         }
29 
30         public override string description()
31         {
32             return base.description()+",奶泡";
33         }
34     }
35 
36     class milk : ingredients //牛奶
37     {
38         public milk(coffee beverage)
39             : base(beverage)
40         {
41         }
42 
43         public override double cost()
44         {
45             return base.cost() + 6;
46         }
47 
48         public override string description()
49         {
50             return base.description() + ",牛奶";
51         }
52     }

progream:

 1 class program
 2     {
 3         static void main(string[] args)
 4         {
 5             coffee b = new espresso();
 6 
 7             coffee b1 = new mocha(b);
 8 
 9             coffee b2 = new whip(b1);
10 
11             coffee b3 = new milk(b2);
12 
13             console.writeline("名称:{0}\n价格:{1}",b3.description (),b3.cost ());
14 
15             console.read();
16         }
17     }
 

以上就是装饰模式的介绍,装饰模式的优点是它的扩展比使用继承更灵活,不会使类很多很多。接下来是对关于coffee的东西,感兴趣的可以看下

 

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网