每个人都会死去,但不是每个人都曾经真正活过
Ever man dies,Not every man really lives
BlogJava
首页
新随笔
新文章
联系
聚合
管理
posts - 7,comments - 0,trackbacks - 0
<
2024年12月
>
日
一
二
三
四
五
六
24
25
26
27
28
29
30
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
some one once said, if you want something very badly, set it free. if it comes back to you, it's yours forever. if it doesn't, it was not yours to begin with.
常用链接
我的随笔
我的评论
我的参与
留言簿
(1)
给我留言
查看公开留言
查看私人留言
随笔分类
java(2)
杂记(2)
生活(1)
随笔档案
2006年10月 (1)
2006年9月 (6)
文章分类
java
收藏(3)
设计模式(3)
文章档案
2006年9月 (3)
搜索
最新评论
阅读排行榜
1. 近期工作学习计划(199)
2. 学习计划(194)
3. 每日流水帐之-------用户配置的生成(184)
4. 每日流水帐之-------初识spring(157)
5. 每日流水帐之-------对myblog的几点新想法(156)
评论排行榜
1. 最近比较烦(0)
2. 每日流水帐之-------用户配置的生成(0)
3. 每日流水帐之-------对myblog的几点新想法(0)
4. 学习计划(0)
5. 每日流水帐之-------初识spring(0)
设计模式学习之(Head First Design Patterns)------装饰模式
Decorator
The Decorator pattern attaches additional responsibilities to an object dynamically . Decorator provide a flexible alternative to subclass for extending functionality .
装饰模式能进行动态地附加额外的职责。装饰模式提供了除子类继承外可供选择的另一种方案。
class should be open for extension , but close for modification.
直接看代码比较容易理解 呵呵
类CondimentDecorator 继承自父类 Beverage (个人认为这是装饰模式的关键)CondimentDecorator 的子类提供了所有“装饰”。
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: Beverage.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
abstract
class
Beverage
{
protected
String description
=
"
Beverage
"
;
public
String getDescription()
{
return
description;
}
public
abstract
double
cost();
}
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: CondimentDecorator.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
abstract
class
CondimentDecorator
extends
Beverage
{
/**/
/*
* @author Wangzg
* Time: 2006-9-18
* MethodsName:
* variable in :
* variable out :
* Description:
*/
public
abstract
String getDescription();
}
类 Espresso ,HouseBlend 也是从Beverage 继承 没什么特殊的地方。
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: Espresso.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
class
Espresso
extends
Beverage
{
/**/
/*
* @author Wangzg Time: 2006-9-18 MethodsName: variable in : variable out :
* Description:
*/
public
double
cost()
{
//
TODO Auto-generated method stub
return
0.1
;
}
public
Espresso()
{
//
TODO Auto-generated constructor stub
description
=
"
Espresso
"
;
}
}
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: HouseBlend.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
class
HouseBlend
extends
Beverage
{
/**/
/*
* @author Wangzg Time: 2006-9-18 MethodsName: variable in : variable out :
* Description:
*/
public
double
cost()
{
//
TODO Auto-generated method stub
return
0.2
;
}
public
HouseBlend()
{
//
TODO Auto-generated constructor stub
description
=
"
HouseBlend
"
;
}
}
类Mocha ,Whip 是CondimentDecorator 的子类,这两个类需要仔细看看。
以Mocha类为例 他的构造函数以一个Beverage的对象为参数然后对Beverage进行“装饰”,由于Mocha是Beverage的子类一切就变的自然了(^ o ^)
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: Mocha.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
class
Mocha
extends
CondimentDecorator
{
private
Beverage beverage;
/**/
/*
* @author Wangzg Time: 2006-9-18 MethodsName: variable in : variable out :
* Description:
*/
public
String getDescription()
{
//
TODO Auto-generated method stub
return
beverage.getDescription()
+
"
Mocha
"
;
}
public
Mocha(Beverage beverage)
{
//
TODO Auto-generated constructor stub
this
.beverage
=
beverage;
}
/**/
/*
* @author Wangzg Time: 2006-9-18 MethodsName: variable in : variable out :
* Description:
*/
public
double
cost()
{
//
TODO Auto-generated method stub
return
beverage.cost()
+
0.1
;
}
}
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: Whip.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
class
Whip
extends
CondimentDecorator
{
private
Beverage beverage;
/**/
/*
* @author Wangzg Time: 2006-9-18 MethodsName: variable in : variable out :
* Description:
*/
public
double
cost()
{
//
TODO Auto-generated method stub
return
beverage.cost()
+
0.2
;
}
public
Whip(Beverage beverage)
{
//
TODO Auto-generated constructor stub
this
.beverage
=
beverage;
}
public
String getDescription()
{
//
TODO Auto-generated method stub
return
beverage.getDescription()
+
"
Whip
"
;
}
}
此为测试代码:
运行的结果应该是
Espresso Whip Mocha 0.4
HouseBlend Whip 0.4
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* FileName: testDecoratorPattern.java
* PackageName: src.decoratorPattern
* Description:
*/
package
src.decoratorPattern;
public
class
testDecoratorPattern
{
/** */
/**
*
@author
Wangzg
* Time: 2006-9-18
* MethodsName:
* var in :
* var out :
* Description:
*/
public
static
void
main(String[] args)
{
Beverage beverage1
=
new
Espresso();
Beverage beverage2
=
new
HouseBlend();
beverage1
=
new
Mocha(
new
Whip(beverage1));
beverage2
=
new
Whip(beverage2);
System.out.println(beverage1.getDescription()
+
"
"
+
beverage1.cost());
System.out.println(beverage2.getDescription()
+
"
"
+
beverage2.cost());
}
}
posted on 2006-09-18 20:44
saftyfirst
阅读(154)
评论(0)
编辑
收藏
所属分类:
设计模式