The recipient of the order
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 was 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.
Command object
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.
The requestor of the command is the issuer
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.
Client code
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
{Create the issuer of the command}
Remote: = tsimpleremotecontrol.create;
{The recipient of the Create command}
Light: = Tlight.create;
Garagedoor: = tgaragedoor.create;
{Create a specific command object}
Lightoncommand: = Tlightoncommand.create (light);
Garagedooropencommand: = Tgaragedooropencommand.create (Garagedoor);
{Load command and execute}
Remote.setcommand (Lightoncommand);
remote.buttonwaspressed;
Remote.setcommand (Garagedooropencommand);
remote.buttonwaspressed;
Remote.free;
Light.free;
Garagedoor.free;
Lightoncommand.free;
Garagedooropencommand.free;
READLN;
End.
Run results
Delphi design mode: "Headfirst design mode" DELPHI7 Code---Command mode simpleremotecontroltest [GO]