Recently, I learned a fans, so I want to write an article about the model. This article is an example of << Java and the model >> (阎 宏), I change it into Delphi. I wrote something I hope everyone can understand.
This example is still better understood, so only the code is given.
UNIT PATTERN;
Interface
Uses dialogs;
TYPE TAUDIOPLAYER = Class;
TCOMMAND = Class Public Procedure EXECUTE ;VIRTUAL;
TPLAYCOMMAND = Class (TCOMMAND) Private Audioplayer: Taudioplayer; Public Procedure EXECUTE; OVERRIDE; Procedure PlayCommand (ap: taudioplayer);
TSTOPCOMMAND = Class (TCOMMAND) Private Audioplayer: Taudioplayer; Public Procedure EXECUTE; OVERRIDE; Procedure Stopcomman (ap: taudioPlayer);
TREWINDCOMMAND = Class (TCOMMAND) Private Audioplayer: TaudioPlayer; Public Procedure EXECUTE; OVERRIDE; Procedure RewindCommand (AP: Taudioplayer);
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 Rew India ();
Tclient = Class Private Keypad: TKEYPAD; Audioplayer: Taudioplayer; PUBLIC Constructor Create (); Procedure Test ();
IMPLEMENTATION
{TKEYPAD}
Constructor tkeypad.create (Playc, Stopc, Rewindc: TCommand); Begin PlayCommand: = Playc; StopCommand: = Stopc; RewindCommand: = REWINDC; END;
Procedure tkeypad.play; begin playcommand.execute;
Procedure tkeypad.rew; begin rebindcommand.execute;
Procedure tkeypad.stop; begin stopcommand.execute;
{Taudioplayer}
Procedure taudioplayer.play; begin showmessage ('play'); end; procedure taudioplayer.rewding; begin showMessage ('rebind');
Procedure taudioplayer.stop; begin showmessage ('stop'); end;
{TPLAYCOMMAND}
Procedure tplayCommand.execute; begin inherited; audioplayer.play ();
Procedure tplaycommand.playcommand (ap: taudioplayer); begin self.audioPlayer: = ap;
{TREWINDCOMMAND}
Procedure TrewindCommand.execute; begin inherited; audioplayer.rew;
Procedure Trewindcommand.rewindcommand (ap: taudioplayer); begin audioplayer: = ap;
{TStopCommand}
Procedure tstopCommand.execute; begin inherited; audioplayer.stop;
Procedure tstopcommand.stopcomman (ap: taudioplayer); begin AudioPlayer: = ap;
{Tclient}
Constructor tclient.create; begin audioplayer: = taudioPlayer.create ();
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; keys;
End.