会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
狗狗的技术小窝
设计模式入门-装饰模式
装饰模式
Person
1
public
class
Person
{
2
3
private
String name;
4
5
public
Person()
{}
6
7
public
Person(String name)
{
8
this
.name
=
name;
9
}
10
11
public
void
show()
{
12
System.out.print(name
+
"
has
"
);
13
}
14
}
Clothe
1
public
class
Clothe
extends
Person
{
2
3
private
Person person;
4
5
public
void
decorate(Person person)
{
6
this
.person
=
person;
7
}
8
9
public
void
show()
{
10
if
(
null
!=
person)
{
11
person.show();
12
}
13
}
14
}
Skirt
1
public
class
Skirt
extends
Clothe
{
2
3
public
void
show()
{
4
super
.show();
5
System.out.print(
"
Skirt
"
);
6
}
7
}
TShirt
1
public
class
TShirt
extends
Clothe
{
2
3
public
void
show()
{
4
super
.show();
5
System.out.print(
"
TShirt
"
);
6
}
7
}
ClotheTest
1
class
ClotheTest
2
{
3
public
static
void
main(String[] args)
4
{
5
Clothe cl
=
new
Clothe();
6
Person person
=
new
Person(
"
XuSama
"
);
7
8
TShirt ts
=
new
TShirt();
9
Skirt sk
=
new
Skirt();
10
11
cl.decorate(person);
12
ts.decorate(cl);
13
sk.decorate(ts);
14
sk.show();
15
16
}
17
}
18
未完待续~~
posted on
2009-01-07 16:50
lostheaven
阅读(
209
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告
导航
博客园
首页
新随笔
联系
订阅
管理