会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
我思考..我存在..
好好学习..天天向上.. 159753258 楼上我老婆,楼下是我! 呼叫中心、小型信息管理系统,电话:13516218837
博客园
首页
新随笔
联系
管理
订阅
Decorator 在程序运行时生成新的操作,这些新的操作是原有的操作的变体
ISimpleWriter
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
Gof.Nan.Test.Decorator
6
{
7
public
interface
ISimpleWriter
8
{
9
void
Write(
char
c);
10
void
Write(
string
s);
11
void
WriteLine();
12
void
Close();
13
}
14
}
SimpleStreamWriter
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
using
System.IO;
5
6
namespace
Gof.Nan.Test.Decorator
7
{
8
public
class
SimpleStreamWriter : StreamWriter,ISimpleWriter
9
{
10
public
SimpleStreamWriter(Stream s)
11
:
base
(s)
12
{
13
}
14
15
public
SimpleStreamWriter(
string
path)
16
:
base
(path)
17
{
18
}
19
}
20
}
OozinozFilter
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
Gof.Nan.Test.Decorator
6
{
7
public
abstract
class
OozinozFilter : ISimpleWriter
8
{
9
protected
ISimpleWriter _writer;
10
11
public
OozinozFilter(ISimpleWriter w)
12
{
13
_writer
=
w;
14
}
15
16
ISimpleWriter 成员
#region
ISimpleWriter 成员
17
18
public
abstract
void
Write(
char
c);
19
20
public
virtual
void
Write(
string
s)
21
{
22
foreach
(
char
c
in
s.ToCharArray())
23
{
24
Write(c);
25
}
26
}
27
28
public
virtual
void
WriteLine()
29
{
30
_writer.WriteLine();
31
}
32
33
public
void
Close()
34
{
35
_writer.Close();
36
}
37
38
#endregion
39
}
40
}
LowerCaseFilter
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
Gof.Nan.Test.Decorator
6
{
7
public
class
LowerCaseFilter : OozinozFilter
8
{
9
public
LowerCaseFilter(ISimpleWriter w)
10
:
base
(w)
11
{
12
}
13
14
public
override
void
Write(
char
c)
15
{
16
_writer.Write(
char
.ToLower(c));
17
}
18
}
19
}
20
客户代码
1
using
System;
2
using
System.Collections.Generic;
3
using
System.Text;
4
5
namespace
Gof.Nan.Test.Decorator
6
{
7
public
class
ShowLowerCase
8
{
9
static
void
Main(
string
[] args)
10
{
11
ISimpleWriter w
=
new
SimpleStreamWriter(
"
sample.txt
"
);
12
ISimpleWriter x
=
new
LowerCaseFilter(w);
13
x.Write(
"
This Text,notably ALL in LoweR CasE!
"
);
14
x.Close();
15
}
16
}
17
}
类图:
The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.
posted @
2007-01-15 16:06
南守拥
阅读(
343
) 评论(
2
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部
公告