Command mode makes it easy to implement undo functionality.
Recipients of the command:
1unit Ureceiveobject;
2
3interface
4
5type
6 tlight = Class (TObject)
7 Public
8 procedure Open;
9 procedure Off;
Ten end;
11
12implementation
13
14{Tlight}
15
16procedure Tlight.off;
17begin
Writeln (' light is off. ');
19end;
20
21procedure Tlight.open;
22begin
Writeln (' light was on. ');
24end;
25
26end.
-
Command object:
1unit Ucommandobject;
2
3interface
4
5uses
6 ureceiveobject;
7
8type
9 TCommand = Class (TObject)
Ten public
Procedure Execute; Virtual Abstract
Procedure Undo; Virtual Abstract
The end;
14
Tlightoncommand = Class (TCommand)
Private
Flight:tlight;
Public
Constructor Create (alight:tlight);
Procedure Execute; Override
Procedure Undo; Override
The end;
23
Tlightoffcommand = Class (TCommand)
Private
Flight:tlight;
Public
Constructor Create (alight:tlight);
Procedure Execute; Override
Procedure Undo; Override
The end;
32
33implementation
34
35{Tlightoncommand}
36
37constructor tlightoncommand.create (alight:tlight);
38begin
FLight: = alight;
40end;
41
42procedure Tlightoncommand.execute;
43begin
Flight.open;
45end;
46
47procedure Tlightoncommand.undo;
48begin
Flight.off;
50end;
51
52{Tlightoffcommand}
53
54constructor tlightoffcommand.create (alight:tlight);
55begin
FLight: = alight;
57end;
58
59procedure Tlightoffcommand.execute;
60begin
Flight.off;
62end;
63
64procedure Tlightoffcommand.undo;
65begin
Flight.open;
67end;
68
69end.
70
Requester of the command:
1unit Usimpleremotewithundo;
2
3interface
4
5uses
6 ucommandobject;
7
8type
9 Tsimpleremotewithundo = Class (TObject)
Private
One by one foncommand:tcommand;
Foffcommand:tcommand;
Fundocommand:tcommand;
Public
Procedure SetCommand (Aoncommand, Aoffcommand:tcommand);
Procedure onbuttonwaspressed;
Procedure offbuttonwaspressed;
Procedure undobuttonwaspressed;
The end;
20
21implementation
22
23{Tsimpleremotewithundo}
24
25procedure tsimpleremotewithundo.offbuttonwaspressed;
26begin
Foffcommand.execute;
Fundocommand: = Foffcommand;
29end;
30
31procedure tsimpleremotewithundo.onbuttonwaspressed;
32begin
Foncommand.execute;
Fundocommand: = Foncommand;
35end;
36
37procedure Tsimpleremotewithundo.setcommand (Aoncommand, Aoffcommand:tcommand);
38begin
Foncommand: = Aoncommand;
Foffcommand: = Aoffcommand;
41end;
42
43procedure tsimpleremotewithundo.undobuttonwaspressed;
44begin
Fundocommand.undo;
46end;
47
48end.
49
Client, create a specific command object:
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
One by one remote:tsimpleremotewithundo;
Light:tlight;
Lightoncommand:tcommand;
Lightoffcommand:tcommand;
15
16begin
+ Remote: = tsimpleremotewithundo.create;
18
Light: = Tlight.create;
20
Lightoncommand: = Tlightoncommand.create (light);
Lightoffcommand: = Tlightoffcommand.create (light);
23
Remote.setcommand (Lightoncommand, Lightoffcommand);
25
remote.onbuttonwaspressed;
remote.offbuttonwaspressed;
remote.undobuttonwaspressed;
Writeln;
remote.offbuttonwaspressed;
remote.onbuttonwaspressed;
remote.undobuttonwaspressed;
33
Remote.free;
Light.free;
Lightoncommand.free;
Panax Notoginseng Lightoffcommand.free;
38
READLN;
40end.
41
Operation Result:
Delphi design mode: "Headfirst design mode" DELPHI7 Code---Command mode simpleremotewithundotest[)