淡泊明志 宁静致远
宠辱不惊 闲看庭前花开花落 去留无意 漫随天外云卷云舒
博客园
首页
新随笔
新文章
联系
订阅
管理
posts - 21,comments - 13,trackbacks - 0
<
2008年1月
>
日
一
二
三
四
五
六
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
在成功的道路上,
如果你没有耐心去等待成功的到来,
那么你只好用一生的耐心去面对失败。
与我联系
发短消息
搜索
常用链接
我的随笔
我的空间
我的短信
我的评论
更多链接
我的参与
我的新闻
最新评论
我的标签
留言簿
给我留言
查看留言
我参与的团队
北京.NET俱乐部(1/1482)
保定.NET俱乐部(0/27)
我的标签
c#
(8)
.Net
(7)
设计
(6)
模式
(6)
GET
(3)
POST
(3)
请求web
(1)
web
(1)
数据
(1)
windows
(1)
更多
随笔分类
C#学习(5)
Delphi学习(1)
Head First 设计模式 读书摘记(5)
SQL学习(4)
WebService学习(1)
读书(7)
管理心得(3)
设计模式学习(1)
随笔档案
2008年9月 (1)
2008年8月 (1)
2008年5月 (4)
2008年4月 (1)
2008年3月 (3)
2008年2月 (2)
2008年1月 (9)
最新评论
1. re: [转]C# 向web网站GET、POST 数据
gb2312编码方式有C++版吗?
--企业即时通讯
阅读排行榜
1. 转载:动态调用WebService(C#)(488)
2. Head First 设计模式 读书摘记(五) 简单工厂模式 C#代码 (314)
3. Head First 设计模式 读书摘记(三) 观察者模式 C#代码(309)
4. 2007年从当当网所购图书(304)
5. Extending SQL 2005 Fulltext Search (转载)(275)
评论排行榜
1. 2007年从当当网所购图书(3)
2. Head First 设计模式 读书摘记(三) 观察者模式 C#代码(2)
3. 感悟(转载)(2)
4. 见“利”忘“义”???(2)
5. [转]C# 向web网站GET、POST 数据(1)
Head First 设计模式 读书摘记(四) 装饰者模式 C#代码
装饰者模式:动态的将责任附加到对象上,若要扩展功能,装饰者提供了比继承更有弹性的替代方案。
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
Tag标签:
.Net
,
c#
,
设计
,
模式
posted on 2008-01-18 15:34
RainWaterLily
阅读(59)
评论(0)
编辑
收藏
所属分类:
Head First 设计模式 读书摘记
、
读书
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2008-03-13 17:31 编辑过
相关文章:
.NET设计模式系列文章
C#设计模式(1)
.NET设计模式(2):单件模式(Singleton Pattern)
.NET设计模式(3):抽象工厂模式(Abstract Factory)
C#下如何实现服务器+客户端的聊天程序
Webservice 的设计和模式
相关链接:
所属分类的其他文章:
Head First 设计模式 读书摘记(五) 简单工厂模式 C#代码
Head First 设计模式 读书摘记(四) 装饰者模式 C#代码
Head First 设计模式 读书摘记(三) 观察者模式 C#代码
Head First 设计模式 读书摘记(二) 策略模式 C#代码
Head First 设计模式 读书摘记(一)OO设计原则
最新IT新闻:
淘宝网合并阿里妈妈 专家称阿里巴巴或有新战略
微软研究院发布 AutoCollage - 整理并融合照片
2008年9月5日科技博客精选
SNS网站风靡影响工作效率 公司下令封杀
《孢子》正式发布
博客园新闻频道
博客园首页
社区