Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest
Posted on 2009-05-03 10:23 都市牧羊 阅读(491) 评论(0) 编辑 收藏 举报命令模式可以很轻松的实现撤销(Undo)功能。
命令的接受者:
命令的接受者
1unit uReceiveObject;
2
3interface
4
5type
6 TLight = class(TObject)
7 public
8 procedure Open;
9 procedure Off;
10 end;
11
12implementation
13
14{ TLight }
15
16procedure TLight.Off;
17begin
18 Writeln('Light is off.');
19end;
20
21procedure TLight.Open;
22begin
23 Writeln('Light is on.');
24end;
25
26end.
27
1unit uReceiveObject;
2
3interface
4
5type
6 TLight = class(TObject)
7 public
8 procedure Open;
9 procedure Off;
10 end;
11
12implementation
13
14{ TLight }
15
16procedure TLight.Off;
17begin
18 Writeln('Light is off.');
19end;
20
21procedure TLight.Open;
22begin
23 Writeln('Light is on.');
24end;
25
26end.
27
命令对象:
命令对象
1unit uCommandObject;
2
3interface
4
5uses
6 uReceiveObject;
7
8type
9 TCommand = class(TObject)
10 public
11 procedure Execute; virtual; abstract;
12 procedure Undo; virtual; abstract;
13 end;
14
15 TLightOnCommand = class(TCommand)
16 private
17 FLight: TLight;
18 public
19 constructor Create(aLight: TLight);
20 procedure Execute; override;
21 procedure Undo; override;
22 end;
23
24 TLightOffCommand = class(TCommand)
25 private
26 FLight: TLight;
27 public
28 constructor Create(aLight: TLight);
29 procedure Execute; override;
30 procedure Undo; override;
31 end;
32
33implementation
34
35{ TLightOnCommand }
36
37constructor TLightOnCommand.Create(aLight: TLight);
38begin
39 FLight := aLight;
40end;
41
42procedure TLightOnCommand.Execute;
43begin
44 FLight.Open;
45end;
46
47procedure TLightOnCommand.Undo;
48begin
49 FLight.Off;
50end;
51
52{ TLightOffCommand }
53
54constructor TLightOffCommand.Create(aLight: TLight);
55begin
56 FLight := aLight;
57end;
58
59procedure TLightOffCommand.Execute;
60begin
61 FLight.Off;
62end;
63
64procedure TLightOffCommand.Undo;
65begin
66 FLight.Open;
67end;
68
69end.
70
1unit uCommandObject;
2
3interface
4
5uses
6 uReceiveObject;
7
8type
9 TCommand = class(TObject)
10 public
11 procedure Execute; virtual; abstract;
12 procedure Undo; virtual; abstract;
13 end;
14
15 TLightOnCommand = class(TCommand)
16 private
17 FLight: TLight;
18 public
19 constructor Create(aLight: TLight);
20 procedure Execute; override;
21 procedure Undo; override;
22 end;
23
24 TLightOffCommand = class(TCommand)
25 private
26 FLight: TLight;
27 public
28 constructor Create(aLight: TLight);
29 procedure Execute; override;
30 procedure Undo; override;
31 end;
32
33implementation
34
35{ TLightOnCommand }
36
37constructor TLightOnCommand.Create(aLight: TLight);
38begin
39 FLight := aLight;
40end;
41
42procedure TLightOnCommand.Execute;
43begin
44 FLight.Open;
45end;
46
47procedure TLightOnCommand.Undo;
48begin
49 FLight.Off;
50end;
51
52{ TLightOffCommand }
53
54constructor TLightOffCommand.Create(aLight: TLight);
55begin
56 FLight := aLight;
57end;
58
59procedure TLightOffCommand.Execute;
60begin
61 FLight.Off;
62end;
63
64procedure TLightOffCommand.Undo;
65begin
66 FLight.Open;
67end;
68
69end.
70
命令的请求者:
命令的请求者
1unit uSimpleRemoteWithUndo;
2
3interface
4
5uses
6 uCommandObject;
7
8type
9 TSimpleRemoteWithUndo = class(TObject)
10 private
11 FOnCommand : TCommand;
12 FOffCommand : TCommand;
13 FUndoCommand: TCommand;
14 public
15 procedure SetCommand(aOnCommand, aOffCommand: TCommand);
16 procedure OnButtonWasPressed;
17 procedure OffButtonWasPressed;
18 procedure UndoButtonWasPressed;
19 end;
20
21implementation
22
23{ TSimpleRemoteWithUndo }
24
25procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
26begin
27 FOffCommand.Execute;
28 FUndoCommand := FOffCommand;
29end;
30
31procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
32begin
33 FOnCommand.Execute;
34 FUndoCommand := FOnCommand;
35end;
36
37procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
38begin
39 FOnCommand := aOnCommand;
40 FOffCommand := aOffCommand;
41end;
42
43procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
44begin
45 FUndoCommand.Undo;
46end;
47
48end.
49
1unit uSimpleRemoteWithUndo;
2
3interface
4
5uses
6 uCommandObject;
7
8type
9 TSimpleRemoteWithUndo = class(TObject)
10 private
11 FOnCommand : TCommand;
12 FOffCommand : TCommand;
13 FUndoCommand: TCommand;
14 public
15 procedure SetCommand(aOnCommand, aOffCommand: TCommand);
16 procedure OnButtonWasPressed;
17 procedure OffButtonWasPressed;
18 procedure UndoButtonWasPressed;
19 end;
20
21implementation
22
23{ TSimpleRemoteWithUndo }
24
25procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
26begin
27 FOffCommand.Execute;
28 FUndoCommand := FOffCommand;
29end;
30
31procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
32begin
33 FOnCommand.Execute;
34 FUndoCommand := FOnCommand;
35end;
36
37procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
38begin
39 FOnCommand := aOnCommand;
40 FOffCommand := aOffCommand;
41end;
42
43procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
44begin
45 FUndoCommand.Undo;
46end;
47
48end.
49
客户端,创建具体的命令对象:
客户端,创建具体的命令
1program pSimpleRemoteWithUndoTest;
2
3{$APPTYPE CONSOLE}
4
5uses
6 uSimpleRemoteWithUndo in 'uSimpleRemoteWithUndo.pas',
7 uCommandObject in 'uCommandObject.pas',
8 uReceiveObject in 'uReceiveObject.pas';
9
10var
11 Remote: TSimpleRemoteWithUndo;
12 Light : TLight;
13 LightOnCommand : TCommand;
14 LightOffCommand: TCommand;
15
16begin
17 Remote := TSimpleRemoteWithUndo.Create;
18
19 Light := TLight.Create;
20
21 LightOnCommand := TLightOnCommand.Create(Light);
22 LightOffCommand := TLightOffCommand.Create(Light);
23
24 Remote.SetCommand(LightOnCommand, LightOffCommand);
25
26 Remote.OnButtonWasPressed;
27 Remote.OffButtonWasPressed;
28 Remote.UndoButtonWasPressed;
29 Writeln;
30 Remote.OffButtonWasPressed;
31 Remote.OnButtonWasPressed;
32 Remote.UndoButtonWasPressed;
33
34 Remote.Free;
35 Light.Free;
36 LightOnCommand.Free;
37 LightOffCommand.Free;
38
39 Readln;
40end.
41
1program pSimpleRemoteWithUndoTest;
2
3{$APPTYPE CONSOLE}
4
5uses
6 uSimpleRemoteWithUndo in 'uSimpleRemoteWithUndo.pas',
7 uCommandObject in 'uCommandObject.pas',
8 uReceiveObject in 'uReceiveObject.pas';
9
10var
11 Remote: TSimpleRemoteWithUndo;
12 Light : TLight;
13 LightOnCommand : TCommand;
14 LightOffCommand: TCommand;
15
16begin
17 Remote := TSimpleRemoteWithUndo.Create;
18
19 Light := TLight.Create;
20
21 LightOnCommand := TLightOnCommand.Create(Light);
22 LightOffCommand := TLightOffCommand.Create(Light);
23
24 Remote.SetCommand(LightOnCommand, LightOffCommand);
25
26 Remote.OnButtonWasPressed;
27 Remote.OffButtonWasPressed;
28 Remote.UndoButtonWasPressed;
29 Writeln;
30 Remote.OffButtonWasPressed;
31 Remote.OnButtonWasPressed;
32 Remote.UndoButtonWasPressed;
33
34 Remote.Free;
35 Light.Free;
36 LightOnCommand.Free;
37 LightOffCommand.Free;
38
39 Readln;
40end.
41
运行结果: