Delphi 設計模式:《HeadFirst設計模式》Delphi7代碼---命令模式之SimpleRemoteControlTest [轉]

來源:互聯網
上載者:User

標籤:des   blog   http   io   ar   os   sp   div   on   

//命令的接受者

unit uReceiveObject;

interface

type
  TLight = class(TObject)
  public
    procedure Open;
    procedure Off;
  end;

  TGarageDoor = class(TObject)
  public
    procedure Up;
    procedure Down;
    procedure Stop;
    procedure LightOn;
    procedure LightOff;
  end;

implementation

{ TLight }

procedure TLight.Off;
begin
  Writeln(‘‘);
end;

procedure TLight.Open;
begin
  Writeln(‘Light is On.‘);
end;

{ TGarageDoor }

procedure TGarageDoor.Down;
begin
  Writeln(‘‘);
end;

procedure TGarageDoor.LightOff;
begin
  Writeln(‘‘);
end;

procedure TGarageDoor.LightOn;
begin
  Writeln(‘‘);
end;

procedure TGarageDoor.Stop;
begin
  Writeln(‘‘);
end;

procedure TGarageDoor.Up;
begin
  Writeln(‘GarageDoor is Open.‘);
end;

end.

 

//命令對象

unit uCommandObject;

interface

uses
  uReceiveObject;

type
  TCommand = class(TObject)
  public
    procedure Execute; virtual; abstract;
  end;

  TLightOnCommand = class(TCommand)
  private
    FLight: TLight;
  public
    constructor Create(aLight: TLight);
    procedure Execute; override;
  end;

  TGarageDoorOpenCommand = class(Tcommand)
  private
    FGarageDoor: TGarageDoor;
  public
    constructor Create(aGarageDoor: TGarageDoor);
    procedure Execute; override;
  end;

implementation

{ TLightOnCommand }

constructor TLightOnCommand.Create(aLight: TLight);
begin
  FLight := aLight;
end;

procedure TLightOnCommand.Execute;
begin
  FLight.Open;
end;

{ TGarageDoorOpenCommand }

constructor TGarageDoorOpenCommand.Create(aGarageDoor: TGarageDoor);
begin
  FGarageDoor := aGarageDoor;
end;

procedure TGarageDoorOpenCommand.Execute;
begin
  FGarageDoor.Up;
end;

end.

 

//命令的要求者即發出者

unit uSimpleRemoteControl;

interface

uses
  uCommandObject;

type
  TSimpleRemoteControl = class(TObject)
  private
    FSlot: TCommand;
  public
    procedure SetCommand(aCommand: TCommand);
    procedure ButtonWasPressed;
  end;

implementation

{ TSimpleRemoteControl }

procedure TSimpleRemoteControl.ButtonWasPressed;
begin
  FSlot.Execute;
end;

procedure TSimpleRemoteControl.SetCommand(aCommand: TCommand);
begin
  FSlot := aCommand;
end;

end.

 

//用戶端代碼

program pSimpleRemoteControlTest;

{$APPTYPE CONSOLE}

uses
  uSimpleRemoteControl in ‘uSimpleRemoteControl.pas‘,
  uCommandObject in ‘uCommandObject.pas‘,
  uReceiveObject in ‘uReceiveObject.pas‘;

var
  Remote: TSimpleRemoteControl;
  Light: TLight;
  GarageDoor: TGarageDoor;
  LightOnCommand: TCommand;
  GarageDoorOpenCommand: TCommand;
  
begin
  {建立命令的發出者}
  Remote := TSimpleRemoteControl.Create;

  {建立命令的接收者}
  Light := TLight.Create;
  GarageDoor := TGarageDoor.Create;

  {建立具體的命令對象}
  LightOnCommand := TLightOnCommand.Create(Light);
  GarageDoorOpenCommand := TGarageDoorOpenCommand.Create(GarageDoor);

  {載入命令並執行}
  Remote.SetCommand(LightOnCommand);
  Remote.ButtonWasPressed;
  Remote.SetCommand(GarageDoorOpenCommand);
  Remote.ButtonWasPressed;

  Remote.Free;
  Light.Free;
  GarageDoor.Free;
  LightOnCommand.Free;
  GarageDoorOpenCommand.Free;
  
  Readln;
end.

//運行結果

 

  

Delphi 設計模式:《HeadFirst設計模式》Delphi7代碼---命令模式之SimpleRemoteControlTest [轉]

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.