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

2020-12-13 15:37

阅读:239

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

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

命令的接受者:

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

 

命令对象:

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

 

命令的请求者:

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

 

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

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

 

运行结果:

soscw.com,搜素材 

 

 

 
 

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

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

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


评论


亲,登录后才可以留言!