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


    //命令的接受者

    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.

    //运行结果

     
     
  • 相关阅读:
    互联网协议入门
    C++解决约瑟夫环(史上最清晰)
    C# 最快的逐一打印斐波那契结果数列的算法
    二叉树的遍历(转载)
    C# 多线程join的用法,等待多个子线程结束后再执行主线程
    剖丁解牛式的快速排序分析
    用CTE结合排名函数rank()删除某列值重复的记录
    Http 头部属性详解
    C# 冒泡排序
    设计模式七大原则之依赖倒转原则
  • 原文地址:https://www.cnblogs.com/0x2D-0x22/p/4076157.html
Copyright © 2020-2023  润新知