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

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

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


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


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

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

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

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