博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之SimpleRemoteControlTest [转]...
阅读量:5339 次
发布时间:2019-06-15

本文共 2814 字,大约阅读时间需要 9 分钟。

//命令的接受者

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.

//运行结果

 

 
 

转载于:https://www.cnblogs.com/0x2D-0x22/p/4076157.html

你可能感兴趣的文章
Python正则表达式
查看>>
Linux进程间通信--命名管道
查看>>
UVa 10970 - Big Chocolate
查看>>
js输出
查看>>
H5多文本换行
查看>>
HAL层三类函数及其作用
查看>>
Odoo 去掉 恼人的 "上午"和"下午"
查看>>
web@h,c小总结
查看>>
java编程思想笔记(一)——面向对象导论
查看>>
Data Structure 基本概念
查看>>
Ubuntu改坏sudoers后无法使用sudo的解决办法
查看>>
NEYC 2017 游记
查看>>
[搬运] 写给 C# 开发人员的函数式编程
查看>>
Python之旅Day14 JQuery部分
查看>>
core--线程池
查看>>
redux-effect
查看>>
Swift和OC混编
查看>>
Android轻量级的开源缓存框架ASimpleCache
查看>>
他山之石:加载图片的一个小问题
查看>>
shell - 常识
查看>>