5 Decorator模式
5.1 概述
Decorator模式,又叫装饰模式,就是给一个对象额外地添加一些职责,以适应更多的变化。
5.2 设计
给一个对象添加职责,可以直接修改这个对象,但是这样就变得很危险。本着最大限度不修改原有代码的编码指导思想,应该对这个对象进行包装,再赋予新的对象额外的职责。就如一个步兵有杀伤敌人的功能,再把他装进战车,就额外拥有移动快、防御强的功能了。
5.3 实现
UML图:
示例代码为:
1
using System;
2
3
namespace Example
4
{
5
/// <summary>
6
/// 示例
7
/// </summary>
8
class Example
9
{
10
/// <summary>
11
/// 应用程序的主入口点。
12
/// </summary>
13
[STAThread]
14
static void Main(string[] args)
15
{
16
//张三今天的计划
17
Console.WriteLine( "张三今天的计划:" ) ;
18
Action ZhangSan = new Eat( new Sleep( new Sport( new Action() ) ) ) ;
19
ZhangSan.Act() ;
20
//李四今天的计划
21
Console.WriteLine( "李四今天的计划:" ) ;
22
Action LiSi = new Sport( new Eat( new Sleep( new Action() ) ) ) ;
23
LiSi.Act() ;
24
}
25
/// <summary>
26
/// 主要的功能:工作
27
/// </summary>
28
public class Action
29
{
30
public virtual void Act()
31
{
32
Console.WriteLine( "工作……" ) ;
33
}
34
}
35
/// <summary>
36
/// 使用Decorator模式,增加额外功能:吃饭
37
/// </summary>
38
public class Eat : Action
39
{
40
private Action action = null ;
41
42
public Eat( Action a )
43
{
44
action = a ;
45
}
46
public void EatRice()
47
{
48
Console.WriteLine( "吃饭……" ) ;
49
}
50
public override void Act()
51
{
52
this.EatRice() ;
53
action.Act() ;
54
}
55
}
56
/// <summary>
57
/// 使用Decorator模式,增加额外功能:睡觉
58
/// </summary>
59
public class Sleep : Action
60
{
61
private Action action = null ;
62
63
public Sleep( Action a )
64
{
65
action = a ;
66
}
67
public void Sleeping()
68
{
69
Console.WriteLine( "睡觉……" ) ;
70
}
71
public override void Act()
72
{
73
this.Sleeping() ;
74
action.Act() ;
75
}
76
}
77
/// <summary>
78
/// 使用Decorator模式,增加额外功能:运动
79
/// </summary>
80
public class Sport : Action
81
{
82
private Action action = null ;
83
84
public Sport( Action a )
85
{
86
action = a ;
87
}
88
public void Sporting()
89
{
90
Console.WriteLine( "运动……" ) ;
91
}
92
public override void Act()
93
{
94
this.Sporting() ;
95
action.Act() ;
96
}
97
}
98
}
99
}
100
using System;2

3
namespace Example4
{5
/// <summary>6
/// 示例7
/// </summary>8
class Example9
{10
/// <summary>11
/// 应用程序的主入口点。12
/// </summary>13
[STAThread]14
static void Main(string[] args)15
{16
//张三今天的计划17
Console.WriteLine( "张三今天的计划:" ) ;18
Action ZhangSan = new Eat( new Sleep( new Sport( new Action() ) ) ) ;19
ZhangSan.Act() ;20
//李四今天的计划21
Console.WriteLine( "李四今天的计划:" ) ;22
Action LiSi = new Sport( new Eat( new Sleep( new Action() ) ) ) ;23
LiSi.Act() ;24
}25
/// <summary>26
/// 主要的功能:工作27
/// </summary>28
public class Action29
{30
public virtual void Act() 31
{32
Console.WriteLine( "工作……" ) ;33
}34
}35
/// <summary>36
/// 使用Decorator模式,增加额外功能:吃饭37
/// </summary>38
public class Eat : Action39
{40
private Action action = null ;41

42
public Eat( Action a )43
{44
action = a ;45
}46
public void EatRice()47
{48
Console.WriteLine( "吃饭……" ) ;49
}50
public override void Act()51
{52
this.EatRice() ;53
action.Act() ;54
}55
}56
/// <summary>57
/// 使用Decorator模式,增加额外功能:睡觉58
/// </summary>59
public class Sleep : Action60
{61
private Action action = null ;62

63
public Sleep( Action a )64
{65
action = a ;66
}67
public void Sleeping()68
{69
Console.WriteLine( "睡觉……" ) ;70
}71
public override void Act()72
{73
this.Sleeping() ;74
action.Act() ;75
}76
}77
/// <summary>78
/// 使用Decorator模式,增加额外功能:运动79
/// </summary>80
public class Sport : Action81
{82
private Action action = null ;83

84
public Sport( Action a )85
{86
action = a ;87
}88
public void Sporting()89
{90
Console.WriteLine( "运动……" ) ;91
}92
public override void Act()93
{94
this.Sporting() ;95
action.Act() ;96
}97
}98
}99
}100




浙公网安备 33010602011771号