标签:des blog http io ar os for sp div


 {《HeadFirst设计模式》之命令模式 }
{《HeadFirst设计模式》之命令模式 } { 本单元中的类为命令的接收者      }
{ 本单元中的类为命令的接收者      } { 编译工具 :Delphi7.0         }
{ 编译工具 :Delphi7.0         } { 联系方式 :guzh-0417@163.com }
{ 联系方式 :guzh-0417@163.com }
 unit uReceiveObject;
unit uReceiveObject;
 interface
interface
 type
type TLight = class(TObject)
  TLight = class(TObject) private
  private FLocation: String;
    FLocation: String; public
  public constructor Create(aLocation: String);
    constructor Create(aLocation: String); procedure Open;
    procedure Open; procedure Off;
    procedure Off; end;
  end;
 TCeilingFan = class(TObject)
  TCeilingFan = class(TObject) private
  private FLevel   : Integer;
    FLevel   : Integer; FLocation: String;
    FLocation: String; function GetSpeed: Integer;
    function GetSpeed: Integer; public
  public constructor Create(aLocation: String);
    constructor Create(aLocation: String); procedure High;
    procedure High; procedure Medium;
    procedure Medium; procedure Low;
    procedure Low; procedure Off;
    procedure Off; property  Speed: Integer read GetSpeed;
    property  Speed: Integer read GetSpeed; end;
  end;
 TGarageDoor = class(TObject)
  TGarageDoor = class(TObject) private
  private FLocation: String;
    FLocation: String; public
  public constructor Create(aLocation: String);
    constructor Create(aLocation: String); procedure Up;
    procedure Up; procedure Down;
    procedure Down; procedure Stop;
    procedure Stop; procedure LightOn;
    procedure LightOn; procedure LightOff;
    procedure LightOff; end;
  end;
 TStereo = class(TObject)
  TStereo = class(TObject) private
  private FLocation: String;
    FLocation: String; public
  public constructor Create(aLocation: String);
    constructor Create(aLocation: String); procedure Play;
    procedure Play; procedure Off;
    procedure Off; procedure SetCD;
    procedure SetCD; procedure SetDVD;
    procedure SetDVD; procedure SetRadio;
    procedure SetRadio; procedure SetVolume(aVolume: Integer);
    procedure SetVolume(aVolume: Integer); end;
  end;
 TTV = class(TObject)
  TTV = class(TObject) private
  private FLocation: String;
    FLocation: String; FChannel : Integer;
    FChannel : Integer; public
  public constructor Create(aLocation: String);
    constructor Create(aLocation: String); procedure Open;
    procedure Open; procedure Off;
    procedure Off; procedure SetInputChannel;
    procedure SetInputChannel; end;
  end;
 THottub = class(TObject)
  THottub = class(TObject) private
  private FOpen: Boolean;
    FOpen: Boolean; FTemp: Integer;
    FTemp: Integer; function  GetTemp: Integer;
    function  GetTemp: Integer; procedure SetTemp(const Value: Integer);
    procedure SetTemp(const Value: Integer); public
  public function  Open: Boolean;
    function  Open: Boolean; function  Off : Boolean;
    function  Off : Boolean; procedure BubblesOpen;
    procedure BubblesOpen; procedure BubblesOff;
    procedure BubblesOff; procedure JetsOpen;
    procedure JetsOpen; procedure JetsOff;
    procedure JetsOff; procedure Heat;
    procedure Heat; procedure Cool;
    procedure Cool; property  Temp: Integer read GetTemp write SetTemp;
    property  Temp: Integer read GetTemp write SetTemp; end;
  end; 
   implementation
implementation
 const
const SPEED_HIGH   = 2;
  SPEED_HIGH   = 2; SPEED_MEDIUM = 1;
  SPEED_MEDIUM = 1; SPEED_LOW    = 0;
  SPEED_LOW    = 0;
 { TLight }
{ TLight }
 constructor TLight.Create(aLocation: String);
constructor TLight.Create(aLocation: String); begin
begin FLocation := aLocation;
  FLocation := aLocation; end;
end;
 procedure TLight.Off;
procedure TLight.Off; begin
begin Writeln(FLocation + ‘Light is off.‘);
  Writeln(FLocation + ‘Light is off.‘); end;
end;
 procedure TLight.Open;
procedure TLight.Open; begin
begin Writeln(FLocation + ‘Light is on.‘);
  Writeln(FLocation + ‘Light is on.‘); end;
end;
 { TCeilingFan }
{ TCeilingFan }
 constructor TCeilingFan.Create(aLocation: String);
constructor TCeilingFan.Create(aLocation: String); begin
begin FLocation := aLocation;
  FLocation := aLocation; end;
end;
 function TCeilingFan.GetSpeed: Integer;
function TCeilingFan.GetSpeed: Integer; begin
begin Result := FLevel;
  Result := FLevel; end;
end;
 procedure TCeilingFan.High;
procedure TCeilingFan.High; begin
begin FLevel := SPEED_HIGH;
  FLevel := SPEED_HIGH; Writeln(FLocation + ‘Ceiling fan is on high.‘);
  Writeln(FLocation + ‘Ceiling fan is on high.‘); end;
end;
 procedure TCeilingFan.Low;
procedure TCeilingFan.Low; begin
begin FLevel := SPEED_LOW;
  FLevel := SPEED_LOW; Writeln(FLocation + ‘Ceiling fan is on low.‘);
  Writeln(FLocation + ‘Ceiling fan is on low.‘); end;
end;
 procedure TCeilingFan.Medium;
procedure TCeilingFan.Medium; begin
begin FLevel := SPEED_MEDIUM;
  FLevel := SPEED_MEDIUM; Writeln(FLocation + ‘Ceiling fan is on medium.‘);
  Writeln(FLocation + ‘Ceiling fan is on medium.‘); end;
end;
 procedure TCeilingFan.Off;
procedure TCeilingFan.Off; begin
begin FLevel := 0;
  FLevel := 0; Writeln(FLocation + ‘Ceiling fan is on off.‘);
  Writeln(FLocation + ‘Ceiling fan is on off.‘); end;
end;
 { TGarageDoor }
{ TGarageDoor }
 constructor TGarageDoor.Create(aLocation: String);
constructor TGarageDoor.Create(aLocation: String); begin
begin FLocation := aLocation;
  FLocation := aLocation; end;
end;
 procedure TGarageDoor.Down;
procedure TGarageDoor.Down; begin
begin Writeln(FLocation + ‘Garage door is down.‘);
  Writeln(FLocation + ‘Garage door is down.‘); end;
end;
 procedure TGarageDoor.LightOff;
procedure TGarageDoor.LightOff; begin
begin Writeln(FLocation + ‘Garage light is off.‘);
  Writeln(FLocation + ‘Garage light is off.‘); end;
end;
 procedure TGarageDoor.LightOn;
procedure TGarageDoor.LightOn; begin
begin Writeln(FLocation + ‘Garage light is on.‘);
  Writeln(FLocation + ‘Garage light is on.‘); end;
end;
 procedure TGarageDoor.Stop;
procedure TGarageDoor.Stop; begin
begin Writeln(FLocation + ‘Garage door is stopped.‘);
  Writeln(FLocation + ‘Garage door is stopped.‘); end;
end;
 procedure TGarageDoor.Up;
procedure TGarageDoor.Up; begin
begin Writeln(FLocation + ‘Garage door is up.‘);
  Writeln(FLocation + ‘Garage door is up.‘); end;
end;
 { TStereo }
{ TStereo }
 constructor TStereo.Create(aLocation: String);
constructor TStereo.Create(aLocation: String); begin
begin FLocation := aLocation;
  FLocation := aLocation; end;
end;
 procedure TStereo.Off;
procedure TStereo.Off; begin
begin Writeln(FLocation + ‘Stereo is off.‘);
  Writeln(FLocation + ‘Stereo is off.‘); end;
end;
 procedure TStereo.Play;
procedure TStereo.Play; begin
begin Writeln(FLocation + ‘Stereo is on.‘);
  Writeln(FLocation + ‘Stereo is on.‘); end;
end;
 procedure TStereo.SetCD;
procedure TStereo.SetCD; begin
begin Writeln(FLocation + ‘Stereo is set for CD input.‘);
  Writeln(FLocation + ‘Stereo is set for CD input.‘); end;
end;
 procedure TStereo.SetDVD;
procedure TStereo.SetDVD; begin
begin Writeln(FLocation + ‘Stereo is set for DVD input.‘);
  Writeln(FLocation + ‘Stereo is set for DVD input.‘); end;
end;
 procedure TStereo.SetRadio;
procedure TStereo.SetRadio; begin
begin Writeln(FLocation + ‘Stereo is set for radio.‘);
  Writeln(FLocation + ‘Stereo is set for radio.‘); end;
end;
 procedure TStereo.SetVolume(aVolume: Integer);
procedure TStereo.SetVolume(aVolume: Integer); begin
begin Writeln(FLocation + ‘Stereo volume set to ‘, aVolume);
  Writeln(FLocation + ‘Stereo volume set to ‘, aVolume); end;
end;
 { TTV }
{ TTV }
 constructor TTV.Create(aLocation: String);
constructor TTV.Create(aLocation: String); begin
begin FLocation := aLocation;
  FLocation := aLocation; end;
end;
 procedure TTV.Off;
procedure TTV.Off; begin
begin Writeln(FLocation + ‘TV is off.‘);
  Writeln(FLocation + ‘TV is off.‘); end;
end;
 procedure TTV.Open;
procedure TTV.Open; begin
begin Writeln(FLocation + ‘TV is on.‘);
  Writeln(FLocation + ‘TV is on.‘); end;
end;
 procedure TTV.SetInputChannel;
procedure TTV.SetInputChannel; begin
begin FChannel := 3;
  FChannel := 3; Writeln(‘Channel is set for VCR.‘);
  Writeln(‘Channel is set for VCR.‘); end;
end;
 { THottub }
{ THottub }
 procedure THottub.BubblesOff;
procedure THottub.BubblesOff; begin
begin if Off then
  if Off then Writeln(‘Hottub is not bubbling.‘);
    Writeln(‘Hottub is not bubbling.‘); end;
end;
 procedure THottub.BubblesOpen;
procedure THottub.BubblesOpen; begin
begin if Open then
  if Open then Writeln(‘Hottub is bubbling.‘);
    Writeln(‘Hottub is bubbling.‘); end;
end;
 procedure THottub.Cool;
procedure THottub.Cool; begin
begin FTemp := 98;
  FTemp := 98; Writeln(‘Hottub is cooling to 98 degrees.‘);
  Writeln(‘Hottub is cooling to 98 degrees.‘); end;
end;
 function THottub.GetTemp: Integer;
function THottub.GetTemp: Integer; begin
begin Result := FTemp;
  Result := FTemp; end;
end;
 procedure THottub.Heat;
procedure THottub.Heat; begin
begin FTemp := 105;
  FTemp := 105; Writeln(‘Hottub is heating to a steaming 105 degrees.‘);
  Writeln(‘Hottub is heating to a steaming 105 degrees.‘); end;
end;
 procedure THottub.JetsOff;
procedure THottub.JetsOff; begin
begin if Off then
  if Off then Writeln(‘Hottub jets are off.‘);
    Writeln(‘Hottub jets are off.‘); end;
end;
 procedure THottub.JetsOpen;
procedure THottub.JetsOpen; begin
begin if Open then
  if Open then Writeln(‘Hottub jets are open.‘);
    Writeln(‘Hottub jets are open.‘); end;
end;
 function THottub.Off:  Boolean;
function THottub.Off:  Boolean; begin
begin FOpen  := False;
  FOpen  := False; Result := FOpen;
  Result := FOpen; end;
end;
 function THottub.Open: Boolean;
function THottub.Open: Boolean; begin
begin FOpen  := True;
  FOpen  := True; Result := FOpen;
  Result := FOpen; end;
end;
 procedure THottub.SetTemp(const Value: Integer);
procedure THottub.SetTemp(const Value: Integer); begin
begin FTemp := Value;
  FTemp := Value; end;
end;
 end.
end.


 {《HeadFirst设计模式》之命令模式     }
{《HeadFirst设计模式》之命令模式     } { 将命令接收者中的动作包装成命令对象 }
{ 将命令接收者中的动作包装成命令对象 } { 编译工具 :Delphi7.0               }
{ 编译工具 :Delphi7.0               } { 联系方式 :guzh-0417@163.com       }
{ 联系方式 :guzh-0417@163.com       }
 unit uCommandObject;
unit uCommandObject;
 interface
interface
 uses
uses uReceiveObject;
  uReceiveObject;
 type
type TCommand = class(TObject)
  TCommand = class(TObject) public
  public procedure Execute; virtual; abstract;
    procedure Execute; virtual; abstract; end;
  end;
 TNoCommand = class(TCommand)
  TNoCommand = class(TCommand) public
  public procedure Execute; override;
    procedure Execute; override; end;
  end;
 TLightOnCommand = class(TCommand)
  TLightOnCommand = class(TCommand) private
  private FLight: TLight;
    FLight: TLight; public
  public constructor Create(aLight: TLight);
    constructor Create(aLight: TLight); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TLightOffCommand = class(TCommand)
  TLightOffCommand = class(TCommand) protected
  protected FLight: TLight;
    FLight: TLight; public
  public constructor Create(aLight: TLight);
    constructor Create(aLight: TLight); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TLivingRoomLightOnCommand = class(TLightOnCommand)
  TLivingRoomLightOnCommand = class(TLightOnCommand) end;
  end;
 TLivingRoomLightOffCommand = class(TLightOffCommand)
  TLivingRoomLightOffCommand = class(TLightOffCommand) end;
  end;
 TKitchenLightOnCommand = class(TLightOnCommand)
  TKitchenLightOnCommand = class(TLightOnCommand) end;
  end;
 TKitchenLightOffCommand = class(TLightOffCommand)
  TKitchenLightOffCommand = class(TLightOffCommand) end;
  end;
 TCeilingFanOnCommand = class(TCommand)
  TCeilingFanOnCommand = class(TCommand) private
  private FCeilingFan: TCeilingFan;
    FCeilingFan: TCeilingFan; public
  public constructor Create(aCeilingFan: TCeilingFan);
    constructor Create(aCeilingFan: TCeilingFan); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TCeilingFanOffCommand = class(TCommand)
  TCeilingFanOffCommand = class(TCommand) private
  private FCeilingFan: TCeilingFan;
    FCeilingFan: TCeilingFan; public
  public constructor Create(aCeilingFan: TCeilingFan);
    constructor Create(aCeilingFan: TCeilingFan); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TGarageDoorUpCommand = class(TCommand)
  TGarageDoorUpCommand = class(TCommand) private
  private FGarageDoor: TGarageDoor;
    FGarageDoor: TGarageDoor; public
  public constructor Create(aGarageDoor: TGarageDoor);
    constructor Create(aGarageDoor: TGarageDoor); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TGarageDoorDownCommand = class(TCommand)
  TGarageDoorDownCommand = class(TCommand) private
  private FGarageDoor: TGarageDoor;
    FGarageDoor: TGarageDoor; public
  public constructor Create(aGarageDoor: TGarageDoor);
    constructor Create(aGarageDoor: TGarageDoor); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TStereoOnWithCDCommand = class(TCommand)
  TStereoOnWithCDCommand = class(TCommand) private
  private FStereo: TStereo;
    FStereo: TStereo; public
  public constructor Create(aStereo: TStereo);
    constructor Create(aStereo: TStereo); procedure Execute; override;
    procedure Execute; override; end;
  end;
 TStereoOffCommand = class(TCommand)
  TStereoOffCommand = class(TCommand) private
  private FStereo: TStereo;
    FStereo: TStereo; public
  public constructor Create(aStereo: TStereo);
    constructor Create(aStereo: TStereo); procedure Execute; override;
    procedure Execute; override; end;
  end;
 THottubOnCommand = class(TCommand)
  THottubOnCommand = class(TCommand) private
  private FHottub: THottub;
    FHottub: THottub; public
  public constructor Create(aHottub: THottub);
    constructor Create(aHottub: THottub); procedure Execute; override;
    procedure Execute; override; end;
  end;
 THottubOffCommand = class(TCommand)
  THottubOffCommand = class(TCommand) private
  private FHottub: THottub;
    FHottub: THottub; public
  public constructor Create(aHottub: THottub);
    constructor Create(aHottub: THottub); procedure Execute; override;
    procedure Execute; override; end;
  end;
 implementation
implementation
 { TNoCommand }
{ TNoCommand }
 procedure TNoCommand.Execute;
procedure TNoCommand.Execute; begin
begin end;
end;
 { TLightOnCommand }
{ TLightOnCommand }
 constructor TLightOnCommand.Create(aLight: TLight);
constructor TLightOnCommand.Create(aLight: TLight); begin
begin FLight := aLight;
  FLight := aLight; end;
end;
 procedure TLightOnCommand.Execute;
procedure TLightOnCommand.Execute; begin
begin FLight.Open;
  FLight.Open; end;
end;
 { TLightOffCommand }
{ TLightOffCommand }
 constructor TLightOffCommand.Create(aLight: TLight);
constructor TLightOffCommand.Create(aLight: TLight); begin
begin FLight := aLight;
  FLight := aLight; end;
end;
 procedure TLightOffCommand.Execute;
procedure TLightOffCommand.Execute; begin
begin FLight.Off;
  FLight.Off; end;
end;


 { TLivingRoomLightOnCommand }
{ TLivingRoomLightOnCommand }
 { TLivingRoomLightOffCommand }
{ TLivingRoomLightOffCommand }
 { TKitchenLightOnCommand }
{ TKitchenLightOnCommand }
 { TKitchenLightOffCommand }
{ TKitchenLightOffCommand }


 { TCeilingFanOnCommand }
{ TCeilingFanOnCommand }
 constructor TCeilingFanOnCommand.Create(aCeilingFan: TCeilingFan);
constructor TCeilingFanOnCommand.Create(aCeilingFan: TCeilingFan); begin
begin FCeilingFan := aCeilingFan;
  FCeilingFan := aCeilingFan; end;
end;
 procedure TCeilingFanOnCommand.Execute;
procedure TCeilingFanOnCommand.Execute; begin
begin FCeilingFan.High;
  FCeilingFan.High; end;
end;
 { TCeilingFanOffCommand }
{ TCeilingFanOffCommand }
 constructor TCeilingFanOffCommand.Create(aCeilingFan: TCeilingFan);
constructor TCeilingFanOffCommand.Create(aCeilingFan: TCeilingFan); begin
begin FCeilingFan := aCeilingFan;
  FCeilingFan := aCeilingFan; end;
end;
 procedure TCeilingFanOffCommand.Execute;
procedure TCeilingFanOffCommand.Execute; begin
begin FCeilingFan.Off;
  FCeilingFan.Off; end;
end;
 { TGarageDoorUpCommand }
{ TGarageDoorUpCommand }
 constructor TGarageDoorUpCommand.Create(aGarageDoor: TGarageDoor);
constructor TGarageDoorUpCommand.Create(aGarageDoor: TGarageDoor); begin
begin FGarageDoor := aGarageDoor;
  FGarageDoor := aGarageDoor; end;
end;
 procedure TGarageDoorUpCommand.Execute;
procedure TGarageDoorUpCommand.Execute; begin
begin FGarageDoor.Up;
  FGarageDoor.Up; FGarageDoor.LightOn;
  FGarageDoor.LightOn; end;
end;
 { TGarageDoorDownCommand }
{ TGarageDoorDownCommand }
 constructor TGarageDoorDownCommand.Create(aGarageDoor: TGarageDoor);
constructor TGarageDoorDownCommand.Create(aGarageDoor: TGarageDoor); begin
begin FGarageDoor := aGarageDoor;
  FGarageDoor := aGarageDoor; end;
end;
 procedure TGarageDoorDownCommand.Execute;
procedure TGarageDoorDownCommand.Execute; begin
begin FGarageDoor.Down;
  FGarageDoor.Down; FGarageDoor.LightOff;
  FGarageDoor.LightOff; end;
end;
 { TStereoOnWithCDCommand }
{ TStereoOnWithCDCommand }
 constructor TStereoOnWithCDCommand.Create(aStereo: TStereo);
constructor TStereoOnWithCDCommand.Create(aStereo: TStereo); begin
begin FStereo := aStereo;
  FStereo := aStereo; end;
end;
 procedure TStereoOnWithCDCommand.Execute;
procedure TStereoOnWithCDCommand.Execute; begin
begin FStereo.Play;
  FStereo.Play; FStereo.SetCD;
  FStereo.SetCD; FStereo.SetVolume(11);
  FStereo.SetVolume(11); end;
end;
 { TStereoOffCommand }
{ TStereoOffCommand }
 constructor TStereoOffCommand.Create(aStereo: TStereo);
constructor TStereoOffCommand.Create(aStereo: TStereo); begin
begin FStereo := aStereo;
  FStereo := aStereo; end;
end;
 procedure TStereoOffCommand.Execute;
procedure TStereoOffCommand.Execute; begin
begin FStereo.Off;
  FStereo.Off; end;
end;
 { THottubOnCommand }
{ THottubOnCommand }
 constructor THottubOnCommand.Create(aHottub: Thottub);
constructor THottubOnCommand.Create(aHottub: Thottub); begin
begin FHottub := aHottub;
  FHottub := aHottub; end;
end;
 procedure THottubOnCommand.Execute;
procedure THottubOnCommand.Execute; begin
begin FHottub.Open;
  FHottub.Open; FHottub.Heat;
  FHottub.Heat; FHottub.BubblesOpen;
  FHottub.BubblesOpen; FHottub.JetsOpen;
  FHottub.JetsOpen; end;
end;
 { THottubOffCommand }
{ THottubOffCommand }
 constructor THottubOffCommand.Create(aHottub: THottub);
constructor THottubOffCommand.Create(aHottub: THottub); begin
begin FHottub := aHottub;
  FHottub := aHottub; end;
end;
 procedure THottubOffCommand.Execute;
procedure THottubOffCommand.Execute; begin
begin FHottub.Cool;
  FHottub.Cool; FHottub.Off;
  FHottub.Off; end;
end;
 end.
end.


 {《HeadFirst设计模式》之命令模式                  }
{《HeadFirst设计模式》之命令模式                  } { 本单元中的类为命令的请求者,向命令对象发出请求,}
{ 本单元中的类为命令的请求者,向命令对象发出请求,} { 命令对象通过委托,执行命令接收者中的动作。      }
{ 命令对象通过委托,执行命令接收者中的动作。      } { 编译工具 :Delphi7.0                            }
{ 编译工具 :Delphi7.0                            } { 联系方式 :guzh-0417@163.com                    }
{ 联系方式 :guzh-0417@163.com                    }
 unit uInvoker;
unit uInvoker;
 interface
interface
 uses
uses uCommandObject;
  uCommandObject;
 type
type TRemoteControl = class(TObject)
  TRemoteControl = class(TObject) private
  private FOnCommands : array of TCommand;
    FOnCommands : array of TCommand; FOffCommands: array of TCommand;
    FOffCommands: array of TCommand; FNoCommand  : TCommand;
    FNoCommand  : TCommand; public
  public constructor Create;
    constructor Create; destructor  Destroy; override;
    destructor  Destroy; override; procedure SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand);
    procedure SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand); procedure OnButtonWasPressed(aSlot: Integer);
    procedure OnButtonWasPressed(aSlot: Integer); procedure OffButtonWasPressed(aSlot: Integer);
    procedure OffButtonWasPressed(aSlot: Integer); end;
  end;
 implementation
implementation
 { TRemoteControl }
{ TRemoteControl }
 constructor TRemoteControl.Create;
constructor TRemoteControl.Create; var
var i: Integer;
  i: Integer; begin
begin SetLength(FOnCommands,  7);
  SetLength(FOnCommands,  7); SetLength(FOffCommands, 7);
  SetLength(FOffCommands, 7);
 FNoCommand := TNoCommand.Create;
  FNoCommand := TNoCommand.Create; for i := 0 to 6 do
  for i := 0 to 6 do begin
  begin FOnCommands [i] := FNoCommand;
    FOnCommands [i] := FNoCommand; FOffCommands[i] := FNoCommand;
    FOffCommands[i] := FNoCommand; end;
  end; end;
end;
 destructor TRemoteControl.Destroy;
destructor TRemoteControl.Destroy; begin
begin FNoCommand.Free;
  FNoCommand.Free; inherited;
  inherited; end;
end;
 procedure TRemoteControl.OffButtonWasPressed(aSlot: Integer);
procedure TRemoteControl.OffButtonWasPressed(aSlot: Integer); begin
begin FOffCommands[aSlot].Execute;
  FOffCommands[aSlot].Execute; end;
end;
 procedure TRemoteControl.OnButtonWasPressed(aSlot: Integer);
procedure TRemoteControl.OnButtonWasPressed(aSlot: Integer); begin
begin FOnCommands [aSlot].Execute;
  FOnCommands [aSlot].Execute; end;
end;
 procedure TRemoteControl.SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand);
procedure TRemoteControl.SetCommand(aSlot: Integer; aOnCommand, aOffCommand: TCommand); begin
begin FOnCommands [aSlot] := aOnCommand;
  FOnCommands [aSlot] := aOnCommand; FOffCommands[aSlot] := aOffCommand;
  FOffCommands[aSlot] := aOffCommand; end;
end;
 end.
end.


 {《HeadFirst设计模式》之命令模式 }
{《HeadFirst设计模式》之命令模式 } { 客户端负责创建具体的命令对象   }
{ 客户端负责创建具体的命令对象   } { 编译工具 :Delphi7.0           }
{ 编译工具 :Delphi7.0           } { 联系方式 :guzh-0417@163.com   }
{ 联系方式 :guzh-0417@163.com   }
 program pRemoteControlTest;
program pRemoteControlTest;
 {$APPTYPE CONSOLE}
{$APPTYPE CONSOLE}
 uses
uses uReceiveObject in ‘uReceiveObject.pas‘,
  uReceiveObject in ‘uReceiveObject.pas‘, uCommandObject in ‘uCommandObject.pas‘,
  uCommandObject in ‘uCommandObject.pas‘, uInvoker in ‘uInvoker.pas‘;
  uInvoker in ‘uInvoker.pas‘;
 var
var RemoteControl  : TRemoteControl;
  RemoteControl  : TRemoteControl;
 LivingRoomLight: TLight;
  LivingRoomLight: TLight; KitchenLight   : TLight;
  KitchenLight   : TLight; CeilingFan     : TCeilingFan;
  CeilingFan     : TCeilingFan; GarageDoor     : TGarageDoor;
  GarageDoor     : TGarageDoor; Stereo         : TStereo;
  Stereo         : TStereo;
 LivingRoomLightOnCommand : TLightOnCommand;
  LivingRoomLightOnCommand : TLightOnCommand; LivingRoomLightOffCommand: TLightOffCommand;
  LivingRoomLightOffCommand: TLightOffCommand; KitchenLightOnCommand    : TLightOnCommand;
  KitchenLightOnCommand    : TLightOnCommand; KitchenLightOffCommand   : TLightOffCommand;
  KitchenLightOffCommand   : TLightOffCommand;
 CeilingFanOnCommand      : TCeilingFanOnCommand;
  CeilingFanOnCommand      : TCeilingFanOnCommand; CeilingFanOffCommand     : TCeilingFanOffCommand;
  CeilingFanOffCommand     : TCeilingFanOffCommand;
 GarageDoorUpCommand      : TGarageDoorUpCommand;
  GarageDoorUpCommand      : TGarageDoorUpCommand; GarageDoorDownCommand    : TGarageDoorDownCommand;
  GarageDoorDownCommand    : TGarageDoorDownCommand;
 StereoOnWithCDCommand    : TStereoOnWithCDCommand;
  StereoOnWithCDCommand    : TStereoOnWithCDCommand; StereoOffCommand         : TStereoOffCommand;
  StereoOffCommand         : TStereoOffCommand;
 begin
begin RemoteControl   := TRemoteControl.Create;
  RemoteControl   := TRemoteControl.Create;
 LivingRoomLight := TLight.Create(‘Living Room‘);
  LivingRoomLight := TLight.Create(‘Living Room‘); KitchenLight    := TLight.Create(‘Kitchen‘);
  KitchenLight    := TLight.Create(‘Kitchen‘); CeilingFan      := TCeilingFan.Create(‘Living Room ‘);
  CeilingFan      := TCeilingFan.Create(‘Living Room ‘); GarageDoor      := TGarageDoor.Create(‘‘);;
  GarageDoor      := TGarageDoor.Create(‘‘);; Stereo          := TStereo.Create(‘Living Room‘);
  Stereo          := TStereo.Create(‘Living Room‘);
 LivingRoomLightOnCommand  := TLightOnCommand.Create(LivingRoomLight);
  LivingRoomLightOnCommand  := TLightOnCommand.Create(LivingRoomLight); LivingRoomLightOffCommand := TLightOffCommand.Create(LivingRoomLight);
  LivingRoomLightOffCommand := TLightOffCommand.Create(LivingRoomLight); KitchenLightOnCommand     := TLightOnCommand.Create(KitchenLight);
  KitchenLightOnCommand     := TLightOnCommand.Create(KitchenLight); KitchenLightOffCommand    := TLightOffCommand.Create(KitchenLight);
  KitchenLightOffCommand    := TLightOffCommand.Create(KitchenLight);
 CeilingFanOnCommand       := TCeilingFanOnCommand.Create(CeilingFan);
  CeilingFanOnCommand       := TCeilingFanOnCommand.Create(CeilingFan); CeilingFanOffCommand      := TCeilingFanOffCommand.Create(CeilingFan);
  CeilingFanOffCommand      := TCeilingFanOffCommand.Create(CeilingFan);
 GarageDoorUpCommand       := TGarageDoorUpCommand.Create(GarageDoor);
  GarageDoorUpCommand       := TGarageDoorUpCommand.Create(GarageDoor); GarageDoorDownCommand     := TGarageDoorDownCommand.Create(GarageDoor);
  GarageDoorDownCommand     := TGarageDoorDownCommand.Create(GarageDoor);
 StereoOnWithCDCommand     := TStereoOnWithCDCommand.Create(Stereo);
  StereoOnWithCDCommand     := TStereoOnWithCDCommand.Create(Stereo); StereoOffCommand          := TStereoOffCommand.Create(Stereo);
  StereoOffCommand          := TStereoOffCommand.Create(Stereo);
 RemoteControl.SetCommand(0, LivingRoomLightOnCommand, LivingRoomLightOffCommand);
  RemoteControl.SetCommand(0, LivingRoomLightOnCommand, LivingRoomLightOffCommand); RemoteControl.SetCommand(1, KitchenLightOnCommand, KitchenLightOffCommand);
  RemoteControl.SetCommand(1, KitchenLightOnCommand, KitchenLightOffCommand); RemoteControl.SetCommand(2, CeilingFanOnCommand, CeilingFanOffCommand);
  RemoteControl.SetCommand(2, CeilingFanOnCommand, CeilingFanOffCommand); RemoteControl.SetCommand(3, StereoOnWithCDCommand, StereoOffCommand);
  RemoteControl.SetCommand(3, StereoOnWithCDCommand, StereoOffCommand); RemoteControl.SetCommand(4, GarageDoorUpCommand, GarageDoorDownCommand);
  RemoteControl.SetCommand(4, GarageDoorUpCommand, GarageDoorDownCommand);
 RemoteControl.OnButtonWasPressed (0);
  RemoteControl.OnButtonWasPressed (0); RemoteControl.OffButtonWasPressed(0);
  RemoteControl.OffButtonWasPressed(0); RemoteControl.OnButtonWasPressed (1);
  RemoteControl.OnButtonWasPressed (1); RemoteControl.OffButtonWasPressed(1);
  RemoteControl.OffButtonWasPressed(1); RemoteControl.OnButtonWasPressed (2);
  RemoteControl.OnButtonWasPressed (2); RemoteControl.OffButtonWasPressed(2);
  RemoteControl.OffButtonWasPressed(2); RemoteControl.OnButtonWasPressed (3);
  RemoteControl.OnButtonWasPressed (3); RemoteControl.OffButtonWasPressed(3);
  RemoteControl.OffButtonWasPressed(3); RemoteControl.OnButtonWasPressed (4);
  RemoteControl.OnButtonWasPressed (4); RemoteControl.OffButtonWasPressed(4);
  RemoteControl.OffButtonWasPressed(4);
 RemoteControl.Free;
  RemoteControl.Free; LivingRoomLight.Free;
  LivingRoomLight.Free; KitchenLight.Free;
  KitchenLight.Free; CeilingFan.Free;
  CeilingFan.Free; GarageDoor.Free;
  GarageDoor.Free; Stereo.Free;
  Stereo.Free; LivingRoomLightOnCommand.Free;
  LivingRoomLightOnCommand.Free; LivingRoomLightOffCommand.Free;
  LivingRoomLightOffCommand.Free; KitchenLightOnCommand.Free;
  KitchenLightOnCommand.Free; KitchenLightOffCommand.Free;
  KitchenLightOffCommand.Free; CeilingFanOnCommand.Free;
  CeilingFanOnCommand.Free; CeilingFanOffCommand.Free;
  CeilingFanOffCommand.Free; GarageDoorUpCommand.Free;
  GarageDoorUpCommand.Free; GarageDoorDownCommand.Free;
  GarageDoorDownCommand.Free; StereoOnWithCDCommand.Free;
  StereoOnWithCDCommand.Free; StereoOffCommand.Free;
  StereoOffCommand.Free;
 Readln;
  Readln; end.
end.
运行结果:

Delphi 设计模式:《HeadFirst设计模式》Delphi7代码---命令模式之RemoteControlTest[转]
标签:des blog http io ar os for sp div
原文地址:http://www.cnblogs.com/0x2D-0x22/p/4076163.html