装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
![]()
Decorator_Patterns
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4![]()
5
namespace Decorator_Patterns
6![]()
![]()
{
7
class Program
8![]()
{
9
static void Main(string[] args)
10![]()
{
11
Beverage beverage = new Espresso();
12![]()
13
Console.WriteLine(beverage.getDescription()
14
+" $"+beverage.cost().ToString());
15![]()
16
Beverage beverage2 = new HouseBlend();
17![]()
18
Console.WriteLine(beverage2.getDescription()
19
+ " $" + beverage2.cost().ToString());
20![]()
21
beverage2 = new Mocha(beverage2);
22
Console.WriteLine(beverage2.getDescription()
23
+ " $" + beverage2.cost().ToString());
24![]()
25
beverage2 = new Mocha(beverage2);
26
Console.WriteLine(beverage2.getDescription()
27
+ " $" + beverage2.cost().ToString());
28![]()
29
beverage2 = new Whip(beverage2);
30
Console.WriteLine(beverage2.getDescription()
31
+ " $" + beverage2.cost().ToString());
32![]()
33![]()
34![]()
35
}
36
}
37![]()
38![]()
/**//// <summary>
39
/// 抽象类 ,饮料,调料都从此继承
40
/// </summary>
41
public abstract class Beverage
42![]()
{
43
protected string description = "Unknown Beverage";
44![]()
45
public virtual string getDescription()
46![]()
{
47
return description;
48
}
49![]()
50
public abstract double cost();
51
}
52![]()
53![]()
/**//// <summary>
54
/// 所有调料的父类,继承自Beverage
55
/// </summary>
56
public abstract class CondimentDecorator : Beverage
57![]()
{
58![]()
/**//// <summary>
59
/// 所有的调料装饰者必须实现此方法
60
/// </summary>
61
/// <returns></returns>
62
public CondimentDecorator()
63![]()
{
64
65
}
66![]()
67
}
68![]()
69![]()
/**//// <summary>
70
/// 浓缩咖啡
71
/// </summary>
72
public class Espresso : Beverage
73![]()
{
74
public Espresso()
75![]()
{
76
description = " Espresso ";
77
}
78![]()
79
public override double cost()
80![]()
{
81
return 1.99;
82
}
83![]()
84
}
85![]()
86
public class HouseBlend : Beverage
87![]()
{
88
public HouseBlend()
89![]()
{
90
description =" House Blend Coffee ";
91
}
92![]()
93
public override double cost()
94![]()
{
95
return 2.98;
96
}
97
}
98![]()
99
public class Mocha : CondimentDecorator
100![]()
{
101
private Beverage beverage;
102
public Mocha(Beverage aBeverage)
103![]()
{
104
105
this.beverage = aBeverage;
106
}
107![]()
108
public override string getDescription()
109![]()
{
110
return beverage.getDescription() + ",Mocha ";
111
}
112![]()
113
public override double cost()
114![]()
{
115
return .20 + beverage.cost();
116
}
117![]()
118![]()
119
}
120![]()
121
public class Soy : CondimentDecorator
122![]()
{
123
private Beverage beverage;
124
public Soy(Beverage beverage)
125![]()
{
126
this.beverage = beverage;
127
}
128![]()
129
public override string getDescription()
130![]()
{
131
return beverage.getDescription() + ",Soy ";
132
}
133![]()
134
public override double cost()
135![]()
{
136
return .25 + beverage.cost();
137
}
138![]()
139![]()
140
}
141![]()
142
public class Whip : CondimentDecorator
143![]()
{
144
private Beverage beverage;
145
public Whip(Beverage beverage)
146![]()
{
147
this.beverage = beverage;
148
}
149![]()
150
public override string getDescription()
151![]()
{
152
return beverage.getDescription() + ",Whip ";
153
}
154![]()
155
public override double cost()
156![]()
{
157
return .32 + beverage.cost();
158
}
159![]()
160![]()
161
}
162![]()
163![]()
164
}
165![]()
1
using System;2
using System.Collections.Generic;3
using System.Text;4

5
namespace Decorator_Patterns6


{7
class Program8

{9
static void Main(string[] args)10

{11
Beverage beverage = new Espresso();12

13
Console.WriteLine(beverage.getDescription() 14
+" $"+beverage.cost().ToString());15

16
Beverage beverage2 = new HouseBlend();17

18
Console.WriteLine(beverage2.getDescription() 19
+ " $" + beverage2.cost().ToString());20

21
beverage2 = new Mocha(beverage2);22
Console.WriteLine(beverage2.getDescription() 23
+ " $" + beverage2.cost().ToString());24

25
beverage2 = new Mocha(beverage2);26
Console.WriteLine(beverage2.getDescription() 27
+ " $" + beverage2.cost().ToString());28

29
beverage2 = new Whip(beverage2);30
Console.WriteLine(beverage2.getDescription() 31
+ " $" + beverage2.cost().ToString());32

33

34

35
}36
}37

38

/**//// <summary>39
/// 抽象类 ,饮料,调料都从此继承40
/// </summary>41
public abstract class Beverage42

{43
protected string description = "Unknown Beverage";44

45
public virtual string getDescription()46

{47
return description;48
}49

50
public abstract double cost();51
}52

53

/**//// <summary>54
/// 所有调料的父类,继承自Beverage55
/// </summary>56
public abstract class CondimentDecorator : Beverage57

{58

/**//// <summary>59
/// 所有的调料装饰者必须实现此方法60
/// </summary>61
/// <returns></returns>62
public CondimentDecorator()63

{64
65
}66

67
}68

69

/**//// <summary>70
/// 浓缩咖啡71
/// </summary>72
public class Espresso : Beverage73

{74
public Espresso()75

{76
description = " Espresso ";77
}78

79
public override double cost()80

{81
return 1.99;82
}83

84
}85

86
public class HouseBlend : Beverage87

{88
public HouseBlend()89

{ 90
description =" House Blend Coffee ";91
}92

93
public override double cost()94

{95
return 2.98;96
}97
}98

99
public class Mocha : CondimentDecorator100

{101
private Beverage beverage;102
public Mocha(Beverage aBeverage)103

{104
105
this.beverage = aBeverage;106
}107

108
public override string getDescription()109

{110
return beverage.getDescription() + ",Mocha ";111
}112

113
public override double cost()114

{115
return .20 + beverage.cost();116
}117

118

119
}120

121
public class Soy : CondimentDecorator122

{123
private Beverage beverage;124
public Soy(Beverage beverage)125

{126
this.beverage = beverage;127
}128

129
public override string getDescription()130

{131
return beverage.getDescription() + ",Soy ";132
}133

134
public override double cost()135

{136
return .25 + beverage.cost();137
}138

139

140
}141

142
public class Whip : CondimentDecorator143

{144
private Beverage beverage;145
public Whip(Beverage beverage)146

{147
this.beverage = beverage;148
}149

150
public override string getDescription()151

{152
return beverage.getDescription() + ",Whip ";153
}154

155
public override double cost()156

{157
return .32 + beverage.cost();158
}159

160

161
}162

163

164
}165

浙公网安备 33010602011771号