码迷,mamicode.com
首页 > Windows程序 > 详细

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]

时间:2014-11-05 14:42:01      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:des   blog   http   io   ar   os   sp   div   on   

命令模式可以很轻松的实现撤销(Undo)功能。

命令的接受者:

bubuko.com,布布扣
 1bubuko.com,布布扣unit uReceiveObject;
 2bubuko.com,布布扣
 3bubuko.com,布布扣interface
 4bubuko.com,布布扣
 5bubuko.com,布布扣type
 6bubuko.com,布布扣  TLight = class(TObject)
 7bubuko.com,布布扣  public
 8bubuko.com,布布扣    procedure Open;
 9bubuko.com,布布扣    procedure Off;
10bubuko.com,布布扣  end;
11bubuko.com,布布扣
12bubuko.com,布布扣implementation
13bubuko.com,布布扣
14bubuko.com,布布扣{ TLight }
15bubuko.com,布布扣
16bubuko.com,布布扣procedure TLight.Off;
17bubuko.com,布布扣begin
18bubuko.com,布布扣  Writeln(‘Light is off.‘);
19bubuko.com,布布扣end;
20bubuko.com,布布扣
21bubuko.com,布布扣procedure TLight.Open;
22bubuko.com,布布扣begin
23bubuko.com,布布扣  Writeln(‘Light is on.‘);
24bubuko.com,布布扣end;
25bubuko.com,布布扣
26bubuko.com,布布扣end.
27bubuko.com,布布扣

 

命令对象:

bubuko.com,布布扣
 1bubuko.com,布布扣unit uCommandObject;
 2bubuko.com,布布扣
 3bubuko.com,布布扣interface
 4bubuko.com,布布扣
 5bubuko.com,布布扣uses
 6bubuko.com,布布扣  uReceiveObject;
 7bubuko.com,布布扣
 8bubuko.com,布布扣type
 9bubuko.com,布布扣  TCommand = class(TObject)
10bubuko.com,布布扣  public
11bubuko.com,布布扣    procedure Execute; virtual; abstract;
12bubuko.com,布布扣    procedure Undo;    virtual; abstract;
13bubuko.com,布布扣  end;
14bubuko.com,布布扣
15bubuko.com,布布扣  TLightOnCommand = class(TCommand)
16bubuko.com,布布扣  private
17bubuko.com,布布扣    FLight: TLight;
18bubuko.com,布布扣  public
19bubuko.com,布布扣    constructor Create(aLight: TLight);
20bubuko.com,布布扣    procedure Execute; override;
21bubuko.com,布布扣    procedure Undo;    override;
22bubuko.com,布布扣  end;
23bubuko.com,布布扣
24bubuko.com,布布扣  TLightOffCommand = class(TCommand)
25bubuko.com,布布扣  private
26bubuko.com,布布扣    FLight: TLight;
27bubuko.com,布布扣  public
28bubuko.com,布布扣    constructor Create(aLight: TLight);
29bubuko.com,布布扣    procedure Execute; override;
30bubuko.com,布布扣    procedure Undo;    override;
31bubuko.com,布布扣  end;
32bubuko.com,布布扣
33bubuko.com,布布扣implementation
34bubuko.com,布布扣
35bubuko.com,布布扣{ TLightOnCommand }
36bubuko.com,布布扣
37bubuko.com,布布扣constructor TLightOnCommand.Create(aLight: TLight);
38bubuko.com,布布扣begin
39bubuko.com,布布扣  FLight := aLight;
40bubuko.com,布布扣end;
41bubuko.com,布布扣
42bubuko.com,布布扣procedure TLightOnCommand.Execute;
43bubuko.com,布布扣begin
44bubuko.com,布布扣  FLight.Open;
45bubuko.com,布布扣end;
46bubuko.com,布布扣
47bubuko.com,布布扣procedure TLightOnCommand.Undo;
48bubuko.com,布布扣begin
49bubuko.com,布布扣  FLight.Off;
50bubuko.com,布布扣end;
51bubuko.com,布布扣
52bubuko.com,布布扣{ TLightOffCommand }
53bubuko.com,布布扣
54bubuko.com,布布扣constructor TLightOffCommand.Create(aLight: TLight);
55bubuko.com,布布扣begin
56bubuko.com,布布扣  FLight := aLight;
57bubuko.com,布布扣end;
58bubuko.com,布布扣
59bubuko.com,布布扣procedure TLightOffCommand.Execute;
60bubuko.com,布布扣begin
61bubuko.com,布布扣  FLight.Off;
62bubuko.com,布布扣end;
63bubuko.com,布布扣
64bubuko.com,布布扣procedure TLightOffCommand.Undo;
65bubuko.com,布布扣begin
66bubuko.com,布布扣  FLight.Open;
67bubuko.com,布布扣end;
68bubuko.com,布布扣
69bubuko.com,布布扣end.
70bubuko.com,布布扣

 

命令的请求者:

bubuko.com,布布扣
 1bubuko.com,布布扣unit uSimpleRemoteWithUndo;
 2bubuko.com,布布扣
 3bubuko.com,布布扣interface
 4bubuko.com,布布扣
 5bubuko.com,布布扣uses
 6bubuko.com,布布扣  uCommandObject;
 7bubuko.com,布布扣
 8bubuko.com,布布扣type
 9bubuko.com,布布扣  TSimpleRemoteWithUndo = class(TObject)
10bubuko.com,布布扣  private
11bubuko.com,布布扣    FOnCommand  : TCommand;
12bubuko.com,布布扣    FOffCommand : TCommand;
13bubuko.com,布布扣    FUndoCommand: TCommand;
14bubuko.com,布布扣  public
15bubuko.com,布布扣    procedure SetCommand(aOnCommand, aOffCommand: TCommand);
16bubuko.com,布布扣    procedure OnButtonWasPressed;
17bubuko.com,布布扣    procedure OffButtonWasPressed;
18bubuko.com,布布扣    procedure UndoButtonWasPressed;
19bubuko.com,布布扣  end;  
20bubuko.com,布布扣
21bubuko.com,布布扣implementation
22bubuko.com,布布扣
23bubuko.com,布布扣{ TSimpleRemoteWithUndo }
24bubuko.com,布布扣
25bubuko.com,布布扣procedure TSimpleRemoteWithUndo.OffButtonWasPressed;
26bubuko.com,布布扣begin
27bubuko.com,布布扣  FOffCommand.Execute;
28bubuko.com,布布扣  FUndoCommand := FOffCommand;
29bubuko.com,布布扣end;
30bubuko.com,布布扣
31bubuko.com,布布扣procedure TSimpleRemoteWithUndo.OnButtonWasPressed;
32bubuko.com,布布扣begin
33bubuko.com,布布扣  FOnCommand.Execute;
34bubuko.com,布布扣  FUndoCommand := FOnCommand;
35bubuko.com,布布扣end;
36bubuko.com,布布扣
37bubuko.com,布布扣procedure TSimpleRemoteWithUndo.SetCommand(aOnCommand, aOffCommand: TCommand);
38bubuko.com,布布扣begin
39bubuko.com,布布扣  FOnCommand  := aOnCommand;
40bubuko.com,布布扣  FOffCommand := aOffCommand;
41bubuko.com,布布扣end;
42bubuko.com,布布扣
43bubuko.com,布布扣procedure TSimpleRemoteWithUndo.UndoButtonWasPressed;
44bubuko.com,布布扣begin
45bubuko.com,布布扣  FUndoCommand.Undo;
46bubuko.com,布布扣end;
47bubuko.com,布布扣
48bubuko.com,布布扣end.
49bubuko.com,布布扣

 

客户端,创建具体的命令对象:

bubuko.com,布布扣
 1bubuko.com,布布扣program pSimpleRemoteWithUndoTest;
 2bubuko.com,布布扣
 3bubuko.com,布布扣{$APPTYPE CONSOLE}
 4bubuko.com,布布扣
 5bubuko.com,布布扣uses
 6bubuko.com,布布扣  uSimpleRemoteWithUndo in ‘uSimpleRemoteWithUndo.pas‘,
 7bubuko.com,布布扣  uCommandObject in ‘uCommandObject.pas‘,
 8bubuko.com,布布扣  uReceiveObject in ‘uReceiveObject.pas‘;
 9bubuko.com,布布扣
10bubuko.com,布布扣var
11bubuko.com,布布扣  Remote: TSimpleRemoteWithUndo;
12bubuko.com,布布扣  Light : TLight;
13bubuko.com,布布扣  LightOnCommand : TCommand;
14bubuko.com,布布扣  LightOffCommand: TCommand;
15bubuko.com,布布扣  
16bubuko.com,布布扣begin
17bubuko.com,布布扣  Remote := TSimpleRemoteWithUndo.Create;
18bubuko.com,布布扣
19bubuko.com,布布扣  Light  := TLight.Create;
20bubuko.com,布布扣
21bubuko.com,布布扣  LightOnCommand  := TLightOnCommand.Create(Light);
22bubuko.com,布布扣  LightOffCommand := TLightOffCommand.Create(Light);
23bubuko.com,布布扣
24bubuko.com,布布扣  Remote.SetCommand(LightOnCommand, LightOffCommand);
25bubuko.com,布布扣
26bubuko.com,布布扣  Remote.OnButtonWasPressed;
27bubuko.com,布布扣  Remote.OffButtonWasPressed;
28bubuko.com,布布扣  Remote.UndoButtonWasPressed;
29bubuko.com,布布扣  Writeln;
30bubuko.com,布布扣  Remote.OffButtonWasPressed;
31bubuko.com,布布扣  Remote.OnButtonWasPressed;
32bubuko.com,布布扣  Remote.UndoButtonWasPressed;
33bubuko.com,布布扣
34bubuko.com,布布扣  Remote.Free;
35bubuko.com,布布扣  Light.Free;
36bubuko.com,布布扣  LightOnCommand.Free;
37bubuko.com,布布扣  LightOffCommand.Free;
38bubuko.com,布布扣  
39bubuko.com,布布扣  Readln;
40bubuko.com,布布扣end.
41bubuko.com,布布扣

 

运行结果:

bubuko.com,布布扣 

 

 

 
 

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest[转]

标签:des   blog   http   io   ar   os   sp   div   on   

原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076159.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!