Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteWithUndoTest
Posted on 2009-05-03 10:23 都市牧羊 阅读(496) 评论(0) 收藏 举报命令模式可以很轻松的实现撤销(Undo)功能。
命令的接受者:

 命令的接受者
命令的接受者1
 unit uReceiveObject;
unit uReceiveObject;2

3
 interface
interface4

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

12
 implementation
implementation13

14
 { TLight }
{ TLight }15

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

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

26
 end.
end.27

命令对象:

 命令对象
命令对象1
 unit uCommandObject;
unit uCommandObject;2

3
 interface
interface4

5
 uses
uses6
 uReceiveObject;
  uReceiveObject;7

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

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

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

33
 implementation
implementation34

35
 { TLightOnCommand }
{ TLightOnCommand }36

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

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

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

52
 { TLightOffCommand }
{ TLightOffCommand }53

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

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

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

69
 end.
end.70

命令的请求者:

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

3
 interface
interface4

5
 uses
uses6
 uCommandObject;
  uCommandObject;7

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

21
 implementation
implementation22

23
 { TSimpleRemoteWithUndo }
{ TSimpleRemoteWithUndo }24

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

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

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

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

48
 end.
end.49

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

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

3
 {$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}4

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

10
 var
var11
 Remote: TSimpleRemoteWithUndo;
  Remote: TSimpleRemoteWithUndo;12
 Light : TLight;
  Light : TLight;13
 LightOnCommand : TCommand;
  LightOnCommand : TCommand;14
 LightOffCommand: TCommand;
  LightOffCommand: TCommand;15
 
  16
 begin
begin17
 Remote := TSimpleRemoteWithUndo.Create;
  Remote := TSimpleRemoteWithUndo.Create;18

19
 Light  := TLight.Create;
  Light  := TLight.Create;20

21
 LightOnCommand  := TLightOnCommand.Create(Light);
  LightOnCommand  := TLightOnCommand.Create(Light);22
 LightOffCommand := TLightOffCommand.Create(Light);
  LightOffCommand := TLightOffCommand.Create(Light);23

24
 Remote.SetCommand(LightOnCommand, LightOffCommand);
  Remote.SetCommand(LightOnCommand, LightOffCommand);25

26
 Remote.OnButtonWasPressed;
  Remote.OnButtonWasPressed;27
 Remote.OffButtonWasPressed;
  Remote.OffButtonWasPressed;28
 Remote.UndoButtonWasPressed;
  Remote.UndoButtonWasPressed;29
 Writeln;
  Writeln;30
 Remote.OffButtonWasPressed;
  Remote.OffButtonWasPressed;31
 Remote.OnButtonWasPressed;
  Remote.OnButtonWasPressed;32
 Remote.UndoButtonWasPressed;
  Remote.UndoButtonWasPressed;33

34
 Remote.Free;
  Remote.Free;35
 Light.Free;
  Light.Free;36
 LightOnCommand.Free;
  LightOnCommand.Free;37
 LightOffCommand.Free;
  LightOffCommand.Free;38
 
  39
 Readln;
  Readln;40
 end.
end.41

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