• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Delphi设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteControl ...

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

 

//命令的接受者

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.

//运行结果


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap