标签:des blog http io ar os sp div on
命令模式可以很轻松的实现撤销(Undo)功能。
命令的接受者:
unit uReceiveObject;
interface
type
TLight = class(TObject)
public
procedure Open;
procedure Off;
end;
implementation
{ TLight }
procedure TLight.Off;
begin
Writeln(‘Light is off.‘);
end;
procedure TLight.Open;
begin
Writeln(‘Light is on.‘);
end;
end.
命令对象:
unit uCommandObject;
interface
uses
uReceiveObject;
type
TCommand = class(TObject)
public
procedure Execute; virtual; abstract;
procedure Undo; virtual; abstract;
end;
TLightOnCommand = class(TCommand)
private
FLight: TLight;
public
constructor Create(aLight: TLight);
procedure Execute; override;
procedure Undo; override;
end;
TLightOffCommand = class(TCommand)
private
FLight: TLight;
public
constructor Create(aLight: TLight);
procedure Execute; override;
procedure Undo; override;
end;
implementation
{ TLightOnCommand }
constructor TLightOnCommand.Create(aLight: TLight);
begin
FLight := aLight;
end;
procedure TLightOnCommand.Execute;
begin
FLight.Open;
end;
procedure TLightOnCommand.Undo;
begin
FLight.Off;
end;
{ TLightOffCommand }
constructor TLightOffCommand.Create(aLight: TLight);
begin
FLight := aLight;
end;
procedure TLightOffCommand.Execute;
begin
FLight.Off;
end;
procedure TLightOffCommand.Undo;
begin
FLight.Open;
end;
end.
命令的请求者:
unit uSimpleRemoteWithUndo;
interface
uses
uCommandObject;
type
TSimpleRemoteWithUndo = class(TObject)
private
FOnCommand : TCommand;
FOffCommand : TCommand;
FUndoCommand: TCommand;
public
procedure SetCommand(aOnCommand, aOffCommand: TCommand);
procedure OnButtonWasPressed;
procedure OffButtonWasPressed;
procedure UndoButtonWasPressed;
end; 
implementation
{ TSimpleRemoteWithUndo }
procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
begin
FOffCommand.Execute;
FUndoCommand := FOffCommand;
end;
procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
begin
FOnCommand.Execute;
FUndoCommand := FOnCommand;
end;
procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
begin
FOnCommand := aOnCommand;
FOffCommand := aOffCommand;
end;
procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
begin
FUndoCommand.Undo;
end;
end.
客户端,创建具体的命令对象:
program pSimpleRemoteWithUndoTest;
{$APPTYPE CONSOLE}
uses
uSimpleRemoteWithUndo in ‘uSimpleRemoteWithUndo.pas‘,
uCommandObject in ‘uCommandObject.pas‘,
uReceiveObject in ‘uReceiveObject.pas‘;
var
Remote: TSimpleRemoteWithUndo;
Light : TLight;
LightOnCommand : TCommand;
LightOffCommand: TCommand;
begin
Remote := TSimpleRemoteWithUndo.Create;
Light := TLight.Create;
LightOnCommand := TLightOnCommand.Create(Light);
LightOffCommand := TLightOffCommand.Create(Light);
Remote.SetCommand(LightOnCommand, LightOffCommand);
Remote.OnButtonWasPressed;
Remote.OffButtonWasPressed;
Remote.UndoButtonWasPressed;
Writeln;
Remote.OffButtonWasPressed;
Remote.OnButtonWasPressed;
Remote.UndoButtonWasPressed;
Remote.Free;
Light.Free;
LightOnCommand.Free;
LightOffCommand.Free;
Readln;
end.
运行结果:
Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]
标签:des blog http io ar os sp div on
原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076159.html