Delphi design mode: "Headfirst design mode" DELPHI7 Code---Command mode simpleremotecontroltest [GO]

Source: Internet
Author: User

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]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.