1

2
{《HeadFirst设计模式》之命令模式 }3
{ 本单元中的类为命令的接收者 }4
{ 编译工具 :Delphi7.0 }5
{ 联系方式 :guzh-0417@163.com }6

7
unit uReceiveObject;8

9
interface10

11
type12
TLight = class(TObject)13
private14
FLocation: String;15
public16
constructor Create(aLocation: String);17
procedure Open;18
procedure Off;19
end;20

21
TCeilingFan = class(TObject)22
private23
FLevel : Integer;24
FLocation: String;25
function GetSpeed: Integer;26
public27
constructor Create(aLocation: String);28
procedure High;29
procedure Medium;30
procedure Low;31
procedure Off;32
property Speed: Integer read GetSpeed;33
end;34

35
TGarageDoor = class(TObject)36
private37
FLocation: String;38
public39
constructor Create(aLocation: String);40
procedure Up;41
procedure Down;42
procedure Stop;43
procedure LightOn;44
procedure LightOff;45
end;46

47
TStereo = class(TObject)48
private49
FLocation: String;50
public51
constructor Create(aLocation: String);52
procedure Play;53
procedure Off;54
procedure SetCD;55
procedure SetDVD;56
procedure SetRadio;57
procedure SetVolume(aVolume: Integer);58
end;59

60
TTV = class(TObject)61
private62
FLocation: String;63
FChannel : Integer;64
public65
constructor Create(aLocation: String);66
procedure Open;67
procedure Off;68
procedure SetInputChannel;69
end;70

71
THottub = class(TObject)72
private73
FOpen: Boolean;74
FTemp: Integer;75
function GetTemp: Integer;76
procedure SetTemp(const Value: Integer);77
public78
function Open: Boolean;79
function Off : Boolean;80
procedure BubblesOpen;81
procedure BubblesOff;82
procedure JetsOpen;83
procedure JetsOff;84
procedure Heat;85
procedure Cool;86
property Temp: Integer read GetTemp write SetTemp;87
end;88
89
implementation90

91
const92
SPEED_HIGH = 2;93
SPEED_MEDIUM = 1;94
SPEED_LOW = 0;95

96
{ TLight }97

98
constructor TLight.Create(aLocation: String);99
begin100
FLocation := aLocation;101
end;102

103
procedure TLight.Off;104
begin105
Writeln(FLocation + 'Light is off.');106
end;107

108
procedure TLight.Open;109
begin110
Writeln(FLocation + 'Light is on.');111
end;112

113
{ TCeilingFan }114

115
constructor TCeilingFan.Create(aLocation: String);116
begin117
FLocation := aLocation;118
end;119

120
function TCeilingFan.GetSpeed: Integer;121
begin122
Result := FLevel;123
end;124

125
procedure TCeilingFan.High;126
begin127
FLevel := SPEED_HIGH;128
Writeln(FLocation + 'Ceiling fan is on high.');129
end;130

131
procedure TCeilingFan.Low;132
begin133
FLevel := SPEED_LOW;134
Writeln(FLocation + 'Ceiling fan is on low.');135
end;136

137
procedure TCeilingFan.Medium;138
begin139
FLevel := SPEED_MEDIUM;140
Writeln(FLocation + 'Ceiling fan is on medium.');141
end;142

143
procedure TCeilingFan.Off;144
begin145
FLevel := 0;146
Writeln(FLocation + 'Ceiling fan is on off.');147
end;148

149
{ TGarageDoor }150

151
constructor TGarageDoor.Create(aLocation: String);152
begin153
FLocation := aLocation;154
end;155

156
procedure TGarageDoor.Down;157
begin158
Writeln(FLocation + 'Garage door is down.');159
end;160

161
procedure TGarageDoor.LightOff;162
begin163
Writeln(FLocation + 'Garage light is off.');164
end;165

166
procedure TGarageDoor.LightOn;167
begin168
Writeln(FLocation + 'Garage light is on.');169
end;170

171
procedure TGarageDoor.Stop;172
begin173
Writeln(FLocation + 'Garage door is stopped.');174
end;175

176
procedure TGarageDoor.Up;177
begin178
Writeln(FLocation + 'Garage door is up.');179
end;180

181
{ TStereo }182

183
constructor TStereo.Create(aLocation: String);184
begin185
FLocation := aLocation;186
end;187

188
procedure TStereo.Off;189
begin190
Writeln(FLocation + 'Stereo is off.');191
end;192

193
procedure TStereo.Play;194
begin195
Writeln(FLocation + 'Stereo is on.');196
end;197

198
procedure TStereo.SetCD;199
begin200
Writeln(FLocation + 'Stereo is set for CD input.');201
end;202

203
procedure TStereo.SetDVD;204
begin205
Writeln(FLocation + 'Stereo is set for DVD input.');206
end;207

208
procedure TStereo.SetRadio;209
begin210
Writeln(FLocation + 'Stereo is set for radio.');211
end;212

213
procedure TStereo.SetVolume(aVolume: Integer);214
begin215
Writeln(FLocation + 'Stereo volume set to ', aVolume);216
end;217

218
{ TTV }219

220
constructor TTV.Create(aLocation: String);221
begin222
FLocation := aLocation;223
end;224

225
procedure TTV.Off;226
begin227
Writeln(FLocation + 'TV is off.');228
end;229

230
procedure TTV.Open;231
begin232
Writeln(FLocation + 'TV is on.');233
end;234

235
procedure TTV.SetInputChannel;236
begin237
FChannel := 3;238
Writeln('Channel is set for VCR.');239
end;240

241
{ THottub }242

243
procedure THottub.BubblesOff;244
begin245
if Off then246
Writeln('Hottub is not bubbling.');247
end;248

249
procedure THottub.BubblesOpen;250
begin251
if Open then252
Writeln('Hottub is bubbling.');253
end;254

255
procedure THottub.Cool;256
begin257
FTemp := 98;258
Writeln('Hottub is cooling to 98 degrees.');259
end;260

261
function THottub.GetTemp: Integer;262
begin263
Result := FTemp;264
end;265

266
procedure THottub.Heat;267
begin268
FTemp := 105;269
Writeln('Hottub is heating to a steaming 105 degrees.');270
end;271

272
procedure THottub.JetsOff;273
begin274
if Off then275
Writeln('Hottub jets are off.');276
end;277

278
procedure THottub.JetsOpen;279
begin280
if Open then281
Writeln('Hottub jets are open.');282
end;283

284
function THottub.Off: Boolean;285
begin286
FOpen := False;287
Result := FOpen;288
end;289

290
function THottub.Open: Boolean;291
begin292
FOpen := True;293
Result := FOpen;294
end;295

296
procedure THottub.SetTemp(const Value: Integer);297
begin298
FTemp := Value;299
end;300

301
end.
1

2
{《HeadFirst设计模式》之命令模式 }3
{ 将命令接收者中的动作包装成命令对象 }4
{ 编译工具 :Delphi7.0 }5
{ 联系方式 :guzh-0417@163.com }6

7
unit uCommandObject;8

9
interface10

11
uses12
uReceiveObject;13

14
type15
TCommand = class(TObject)16
public17
procedure Execute; virtual; abstract;18
end;19

20
TNoCommand = class(TCommand)21
public22
procedure Execute; override;23
end;24

25
TLightOnCommand = class(TCommand)26
private27
FLight: TLight;28
public29
constructor Create(aLight: TLight);30
procedure Execute; override;31
end;32

33
TLightOffCommand = class(TCommand)34
protected35
FLight: TLight;36
public37
constructor Create(aLight: TLight);38
procedure Execute; override;39
end;40

41
TLivingRoomLightOnCommand = class(TLightOnCommand)42
end;43

44
TLivingRoomLightOffCommand = class(TLightOffCommand)45
end;46

47
TKitchenLightOnCommand = class(TLightOnCommand)48
end;49

50
TKitchenLightOffCommand = class(TLightOffCommand)51
end;52

53
TCeilingFanOnCommand = class(TCommand)54
private55
FCeilingFan: TCeilingFan;56
public57
constructor Create(aCeilingFan: TCeilingFan);58
procedure Execute; override;59
end;60

61
TCeilingFanOffCommand = class(TCommand)62
private63
FCeilingFan: TCeilingFan;64
public65
constructor Create(aCeilingFan: TCeilingFan);66
procedure Execute; override;67
end;68

69
TGarageDoorUpCommand = class(TCommand)70
private71
FGarageDoor: TGarageDoor;72
public73
constructor Create(aGarageDoor: TGarageDoor);74
procedure Execute; override;75
end;76

77
TGarageDoorDownCommand = class(TCommand)78
private79
FGarageDoor: TGarageDoor;80
public81
constructor Create(aGarageDoor: TGarageDoor);82
procedure Execute; override;83
end;84

85
TStereoOnWithCDCommand = class(TCommand)86
private87
FStereo: TStereo;88
public89
constructor Create(aStereo: TStereo);90
procedure Execute; override;91
end;92

93
TStereoOffCommand = class(TCommand)94
private95
FStereo: TStereo;96
public97
constructor Create(aStereo: TStereo);98
procedure Execute; override;99
end;100

101
THottubOnCommand = class(TCommand)102
private103
FHottub: THottub;104
public105
constructor Create(aHottub: THottub);106
procedure Execute; override;107
end;108

109
THottubOffCommand = class(TCommand)110
private111
FHottub: THottub;112
public113
constructor Create(aHottub: THottub);114
procedure Execute; override;115
end;116

117
implementation118

119
{ TNoCommand }120

121
procedure TNoCommand.Execute;122
begin123
end;124

125
{ TLightOnCommand }126

127
constructor TLightOnCommand.Create(aLight: TLight);128
begin129
FLight := aLight;130
end;131

132
procedure TLightOnCommand.Execute;133
begin134
FLight.Open;135
end;136

137
{ TLightOffCommand }138

139
constructor TLightOffCommand.Create(aLight: TLight);140
begin141
FLight := aLight;142
end;143

144
procedure TLightOffCommand.Execute;145
begin146
FLight.Off;147
end;148

149

150

151
{ TLivingRoomLightOnCommand }152

153
{ TLivingRoomLightOffCommand }154

155
{ TKitchenLightOnCommand }156

157
{ TKitchenLightOffCommand }158

159

160

161
{ TCeilingFanOnCommand }162

163
constructor TCeilingFanOnCommand.Create(aCeilingFan: TCeilingFan);164
begin165
FCeilingFan := aCeilingFan;166
end;167

168
procedure TCeilingFanOnCommand.Execute;169
begin170
FCeilingFan.High;171
end;172

173
{ TCeilingFanOffCommand }174

175
constructor TCeilingFanOffCommand.Create(aCeilingFan: TCeilingFan);176
begin177
FCeilingFan := aCeilingFan;178
end;179

180
procedure TCeilingFanOffCommand.Execute;181
begin182
FCeilingFan.Off;183
end;184

185
{ TGarageDoorUpCommand }186

187
constructor TGarageDoorUpCommand.Create(aGarageDoor: TGarageDoor);188
begin189
FGarageDoor := aGarageDoor;190
end;191

192
procedure TGarageDoorUpCommand.Execute;193
begin194
FGarageDoor.Up;195
FGarageDoor.LightOn;196
end;197

198
{ TGarageDoorDownCommand }199

200
constructor TGarageDoorDownCommand.Create(aGarageDoor: TGarageDoor);201
begin202
FGarageDoor := aGarageDoor;203
end;204

205
procedure TGarageDoorDownCommand.Execute;206
begin207
FGarageDoor.Down;208
FGarageDoor.LightOff;209
end;210

211
{ TStereoOnWithCDCommand }212

213
constructor TStereoOnWithCDCommand.Create(aStereo: TStereo);214
begin215
FStereo := aStereo;216
end;217

218
procedure TStereoOnWithCDCommand.Execute;219
begin220
FStereo.Play;221
FStereo.SetCD;222
FStereo.SetVolume(11);223
end;224

225
{ TStereoOffCommand }226

227
constructor TStereoOffCommand.Create(aStereo: TStereo);228
begin229
FStereo := aStereo;230
end;231

232
procedure TStereoOffCommand.Execute;233
begin234
FStereo.Off;235
end;236

237
{ THottubOnCommand }238

239
constructor THottubOnCommand.Create(aHottub: Thottub);240
begin241
FHottub := aHottub;242
end;243

244
procedure THottubOnCommand.Execute;245
begin246
FHottub.Open;247
FHottub.Heat;248
FHottub.BubblesOpen;249
FHottub.JetsOpen;250
end;251

252
{ THottubOffCommand }253

254
constructor THottubOffCommand.Create(aHottub: THottub);255
begin256
FHottub := aHottub;257
end;258

259
procedure THottubOffCommand.Execute;260
begin261
FHottub.Cool;262
FHottub.Off;263
end;264

265
end.
1

2
{《HeadFirst设计模式》之命令模式 }3
{ 本单元中的类为命令的请求者,向命令对象发出请求,}4
{ 命令对象通过委托,执行命令接收者中的动作。 }5
{ 编译工具 :Delphi7.0 }6
{ 联系方式 :guzh-0417@163.com }7

8
unit uInvoker;9

10
interface11

12
uses13
uCommandObject;14

15
type16
TRemoteControl = class(TObject)17
private18
FOnCommands : array of TCommand;19
FOffCommands: array of TCommand;20
FNoCommand : TCommand;21
public22
constructor Create;23
destructor Destroy; override;24
procedure SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand);25
procedure OnButtonWasPressed(aSlot: Integer);26
procedure OffButtonWasPressed(aSlot: Integer);27
end;28

29
implementation30

31
{ TRemoteControl }32

33
constructor TRemoteControl.Create;34
var35
i: Integer;36
begin37
SetLength(FOnCommands, 7);38
SetLength(FOffCommands, 7);39

40
FNoCommand := TNoCommand.Create;41
for i := 0 to 6 do42
begin43
FOnCommands [i] := FNoCommand;44
FOffCommands[i] := FNoCommand;45
end;46
end;47

48
destructor TRemoteControl.Destroy;49
begin50
FNoCommand.Free;51
inherited;52
end;53

54
procedure TRemoteControl.OffButtonWasPressed(aSlot: Integer);55
begin56
FOffCommands[aSlot].Execute;57
end;58

59
procedure TRemoteControl.OnButtonWasPressed(aSlot: Integer);60
begin61
FOnCommands [aSlot].Execute;62
end;63

64
procedure TRemoteControl.SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand);65
begin66
FOnCommands [aSlot] := aOnCommand;67
FOffCommands[aSlot] := aOffCommand;68
end;69

70
end.
1

2
{《HeadFirst设计模式》之命令模式 }3
{ 客户端负责创建具体的命令对象 }4
{ 编译工具 :Delphi7.0 }5
{ 联系方式 :guzh-0417@163.com }6

7
program pRemoteControlTest;8

9
{$APPTYPE CONSOLE}10

11
uses12
uReceiveObject in 'uReceiveObject.pas',13
uCommandObject in 'uCommandObject.pas',14
uInvoker in 'uInvoker.pas';15

16
var17
RemoteControl : TRemoteControl;18

19
LivingRoomLight: TLight;20
KitchenLight : TLight;21
CeilingFan : TCeilingFan;22
GarageDoor : TGarageDoor;23
Stereo : TStereo;24

25
LivingRoomLightOnCommand : TLightOnCommand;26
LivingRoomLightOffCommand: TLightOffCommand;27
KitchenLightOnCommand : TLightOnCommand;28
KitchenLightOffCommand : TLightOffCommand;29

30
CeilingFanOnCommand : TCeilingFanOnCommand;31
CeilingFanOffCommand : TCeilingFanOffCommand;32

33
GarageDoorUpCommand : TGarageDoorUpCommand;34
GarageDoorDownCommand : TGarageDoorDownCommand;35

36
StereoOnWithCDCommand : TStereoOnWithCDCommand;37
StereoOffCommand : TStereoOffCommand;38

39
begin40
RemoteControl := TRemoteControl.Create;41

42
LivingRoomLight := TLight.Create('Living Room');43
KitchenLight := TLight.Create('Kitchen');44
CeilingFan := TCeilingFan.Create('Living Room ');45
GarageDoor := TGarageDoor.Create('');;46
Stereo := TStereo.Create('Living Room');47

48
LivingRoomLightOnCommand := TLightOnCommand.Create(LivingRoomLight);49
LivingRoomLightOffCommand := TLightOffCommand.Create(LivingRoomLight);50
KitchenLightOnCommand := TLightOnCommand.Create(KitchenLight);51
KitchenLightOffCommand := TLightOffCommand.Create(KitchenLight);52

53
CeilingFanOnCommand := TCeilingFanOnCommand.Create(CeilingFan);54
CeilingFanOffCommand := TCeilingFanOffCommand.Create(CeilingFan);55

56
GarageDoorUpCommand := TGarageDoorUpCommand.Create(GarageDoor);57
GarageDoorDownCommand := TGarageDoorDownCommand.Create(GarageDoor);58

59
StereoOnWithCDCommand := TStereoOnWithCDCommand.Create(Stereo);60
StereoOffCommand := TStereoOffCommand.Create(Stereo);61

62
RemoteControl.SetCommand(0, LivingRoomLightOnCommand, LivingRoomLightOffCommand);63
RemoteControl.SetCommand(1, KitchenLightOnCommand, KitchenLightOffCommand);64
RemoteControl.SetCommand(2, CeilingFanOnCommand, CeilingFanOffCommand);65
RemoteControl.SetCommand(3, StereoOnWithCDCommand, StereoOffCommand);66
RemoteControl.SetCommand(4, GarageDoorUpCommand, GarageDoorDownCommand);67

68
RemoteControl.OnButtonWasPressed (0);69
RemoteControl.OffButtonWasPressed(0);70
RemoteControl.OnButtonWasPressed (1);71
RemoteControl.OffButtonWasPressed(1);72
RemoteControl.OnButtonWasPressed (2);73
RemoteControl.OffButtonWasPressed(2);74
RemoteControl.OnButtonWasPressed (3);75
RemoteControl.OffButtonWasPressed(3);76
RemoteControl.OnButtonWasPressed (4);77
RemoteControl.OffButtonWasPressed(4);78

79
RemoteControl.Free;80
LivingRoomLight.Free;81
KitchenLight.Free;82
CeilingFan.Free;83
GarageDoor.Free;84
Stereo.Free;85
LivingRoomLightOnCommand.Free;86
LivingRoomLightOffCommand.Free;87
KitchenLightOnCommand.Free;88
KitchenLightOffCommand.Free;89
CeilingFanOnCommand.Free;90
CeilingFanOffCommand.Free;91
GarageDoorUpCommand.Free;92
GarageDoorDownCommand.Free;93
StereoOnWithCDCommand.Free;94
StereoOffCommand.Free;95

96
Readln;97
end.
运行结果:
浙公网安备 33010602011771号