Delphi Command mode
Last Update:2018-12-08
Source: Internet
Author: User
This example is quite understandable, so only the code is provided.
Unit pattern;
Interface
Uses Dialogs;
Type
TAudioPlayer = class;
TCommand = class
Public
Procedure execute; virtual; abstract;
End;
TPlayCommand = class (TCommand)
Private
AudioPlayer: TAudioPlayer;
Public
Procedure execute; override;
Procedure Playcommand (AP: TAudioPlayer );
End;
TStopCommand = class (TCommand)
Private
AudioPlayer: TAudioPlayer;
Public
Procedure execute; override;
Procedure StopComman (AP: TAudioPlayer );
End;
TRewindCommand = class (TCommand)
Private
AudioPlayer: TAudioPlayer;
Public
Procedure execute; override;
Procedure RewindCommand (AP: TAudioPlayer );
End;
TKeyPad = class
Private
PlayCommand: TCommand;
StopCommand: TCommand;
RewindCommand: TCommand;
Public
Constructor Create (PlayC, StopC, RewindC: TCommand); virtual;
Procedure play ();
Procedure stop ();
Procedure rewind ();
End;
TAudioPlayer = class
Public
Procedure play ();
Procedure stop ();
Procedure rewind ();
End;
TClient = class
Private
KeyPad: TKeyPad;
AudioPlayer: TAudioPlayer;
Public
Constructor Create ();
Procedure test ();
End;
Implementation
{TKeyPad}
Constructor TKeyPad. Create (PlayC, StopC, RewindC: TCommand );
Begin
PlayCommand: = PlayC;
StopCommand: = StopC;
RewindCommand: = RewindC;
End;
Procedure TKeyPad. play;
Begin
PlayCommand.exe cute;
End;
Procedure TKeyPad. rewind;
Begin
RewindCommand.exe cute;
End;
Procedure TKeyPad. stop;
Begin
StopCommand.exe cute;
End;
{TAudioPlayer}
Procedure TAudioPlayer. play;
Begin
ShowMessage ('play ');
End;
Procedure TAudioPlayer. rewind;
Begin
ShowMessage ('rewind ');
End;
Procedure TAudioPlayer. stop;
Begin
ShowMessage ('stop ');
End;
{TPlayCommand}
Procedure TPlayCommand.exe cute;
Begin
Inherited;
AudioPlayer. play ();
End;
Procedure TPlayCommand. Playcommand (AP: TAudioPlayer );
Begin
Self. AudioPlayer: = AP;
End;
{TRewindCommand}
Procedure TRewindCommand.exe cute;
Begin
Inherited;
AudioPlayer. Rewind;
End;
Procedure TRewindCommand. RewindCommand (AP: TAudioPlayer );
Begin
AudioPlayer: = ap;
End;
{TStopCommand}
Procedure TStopCommand.exe cute;
Begin
Inherited;
AudioPlayer. Stop;
End;
Procedure TStopCommand. StopComman (AP: TAudioPlayer );
Begin
AudioPlayer: = ap;
End;
{TClient}
Constructor TClient. Create;
Begin
AudioPlayer: = TAudioPlayer. Create ();
End;
Procedure TClient. test;
Var
PlayCommand: TCommand;
StopCommand: TCommand;
RewindCommand: TCommand;
Begin
PlayCommand: = TPlayCommand. Create;
StopCommand: = TStopCommand. Create;
RewindCommand: = TRewindCommand. Create;
KeyPad: = TKeyPad. Create (PlayCommand, StopCommand, RewindCommand );
KeyPad. stop;
KeyPad. play;
KeyPad. rewind;
KeyPad. Stop;
End;
End.