命令模式可以很轻松的实现撤销(Undo)功能。
命令的接受者:
1
unit uReceiveObject;2

3
interface4

5
type6
TLight = class(TObject)7
public8
procedure Open;9
procedure Off;10
end;11

12
implementation13

14
{ TLight }15

16
procedure TLight.Off;17
begin18
Writeln('Light is off.');19
end;20

21
procedure TLight.Open;22
begin23
Writeln('Light is on.');24
end;25

26
end.27

命令对象:
1
unit uCommandObject;2

3
interface4

5
uses6
uReceiveObject;7

8
type9
TCommand = class(TObject)10
public11
procedure Execute; virtual; abstract;12
procedure Undo; virtual; abstract;13
end;14

15
TLightOnCommand = class(TCommand)16
private17
FLight: TLight;18
public19
constructor Create(aLight: TLight);20
procedure Execute; override;21
procedure Undo; override;22
end;23

24
TLightOffCommand = class(TCommand)25
private26
FLight: TLight;27
public28
constructor Create(aLight: TLight);29
procedure Execute; override;30
procedure Undo; override;31
end;32

33
implementation34

35
{ TLightOnCommand }36

37
constructor TLightOnCommand.Create(aLight: TLight);38
begin39
FLight := aLight;40
end;41

42
procedure TLightOnCommand.Execute;43
begin44
FLight.Open;45
end;46

47
procedure TLightOnCommand.Undo;48
begin49
FLight.Off;50
end;51

52
{ TLightOffCommand }53

54
constructor TLightOffCommand.Create(aLight: TLight);55
begin56
FLight := aLight;57
end;58

59
procedure TLightOffCommand.Execute;60
begin61
FLight.Off;62
end;63

64
procedure TLightOffCommand.Undo;65
begin66
FLight.Open;67
end;68

69
end.70

命令的请求者:
1
unit uSimpleRemoteWithUndo;2

3
interface4

5
uses6
uCommandObject;7

8
type9
TSimpleRemoteWithUndo = class(TObject)10
private11
FOnCommand : TCommand;12
FOffCommand : TCommand;13
FUndoCommand: TCommand;14
public15
procedure SetCommand(aOnCommand, aOffCommand: TCommand);16
procedure OnButtonWasPressed;17
procedure OffButtonWasPressed;18
procedure UndoButtonWasPressed;19
end; 20

21
implementation22

23
{ TSimpleRemoteWithUndo }24

25
procedure TSimpleRemoteWithUndo.OffButtonWasPressed;26
begin27
FOffCommand.Execute;28
FUndoCommand := FOffCommand;29
end;30

31
procedure TSimpleRemoteWithUndo.OnButtonWasPressed;32
begin33
FOnCommand.Execute;34
FUndoCommand := FOnCommand;35
end;36

37
procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);38
begin39
FOnCommand := aOnCommand;40
FOffCommand := aOffCommand;41
end;42

43
procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;44
begin45
FUndoCommand.Undo;46
end;47

48
end.49

客户端,创建具体的命令对象:
1
program pSimpleRemoteWithUndoTest;2

3
{$APPTYPE CONSOLE}4

5
uses6
uSimpleRemoteWithUndo in 'uSimpleRemoteWithUndo.pas',7
uCommandObject in 'uCommandObject.pas',8
uReceiveObject in 'uReceiveObject.pas';9

10
var11
Remote: TSimpleRemoteWithUndo;12
Light : TLight;13
LightOnCommand : TCommand;14
LightOffCommand: TCommand;15
16
begin17
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;40
end.41

运行结果:
浙公网安备 33010602011771号