这个例子还是比较好理解的,所以只给出代码.
padrão de unidade;
interface
usaDialogs;
tipo
TAudioPlayer=classe;
TCommand = classe
público
procedimentoexecutar;virtual;resumo;
fim;
TPlayCommand=class(TCommand)
privado
AudioPlayer:TAudioPlayer;
público
procedimentoexecutar;substituir;
procedimentoPlaycommand(AP:TAudioPlayer);
fim;
TStopCommand=class(TCommand)
privado
AudioPlayer:TAudioPlayer;
público
procedimentoexecutar;substituir;
procedimentoStopComman(AP:TAudioPlayer);
fim;
TRewindCommand=class(TCommand)
privado
AudioPlayer:TAudioPlayer;
público
procedimentoexecutar;substituir;
procedimentoRewindCommand(AP:TAudioPlayer);
fim;
TKeyPad=classe
privado
PlayCommand:TCommand;
StopCommand:TCommand;
RebobinarCommand:TCommand;
público
construtorCreate(PlayC,StopC,RewindC:TCommand);virtual;
procedimentoplay();
procedimentosparar();
procedimentorebobinar();
fim;
TAudioPlayer=classe
público
procedimentoplay();
procedimentosparar();
procedimentorebobinar();
fim;
TClient=classe
privado
Teclado:TKeyPad;
AudioPlayer:TAudioPlayer;
público
construtorCreate();
procedimentoteste();
fim;
implementação
{TKeyPad}
construtorTKeyPad.Create(PlayC,StopC,RewindC:TCommand);
começar
PlayCommand:=PlayC;
StopCommand:=StopC;
RebobinarCommand:=RebobinarC;
fim;
procedimentoTKeyPad.play;
começar
PlayCommand.execute;
fim;
procedimentoTKeyPad.rewind;
começar
RewindCommand.execute;
fim;
procedimentoTKeyPad.stop;
começar
StopCommand.execute;
fim;
{TAudioPlayer}
procedimentoTAudioPlayer.play;
começar
ShowMessage(´reproduzir´);
fim;
procedimentoTAudioPlayer.rewind;
começar
ShowMessage(´retroceder´);
fim;
procedimentoTAudioPlayer.stop;
começar
ShowMessage(´parar´);
fim;
{TPlayCommand}
procedimentoTPlayCommand.execute;
começar
herdado;
AudioPlayer.play();
fim;
procedimentoTPlayCommand.Playcommand(AP:TAudioPlayer);
começar
self.AudioPlayer:=AP;
fim;
{TRewindCommand}
procedimentoTRewindCommand.execute;
começar
herdado;
AudioPlayer.Rebobinar;
fim;
procedimentoTRewindCommand.RewindCommand(AP:TAudioPlayer);
começar
Leitor de áudio:=ap;
fim;
{TStopCommand}
procedimentoTStopCommand.execute;
começar
herdado;
AudioPlayer.Stop;
fim;
procedimentoTStopCommand.StopComman(AP:TAudioPlayer);
começar
Leitor de áudio:=ap;
fim;
{TCliente}
construtorTClient.Create;
começar
AudioPlayer:=TAudioPlayer.Create();
fim;
procedimentoTClient.test;
var
PlayCommand:TCommand;
StopCommand:TCommand;
RebobinarCommand:TCommand;
começar
PlayCommand:=TPlayCommand.Create;
StopCommand:=TStopCommand.Create;
RewindCommand:=TRewindCommand.Create;
KeyPad:=TKeyPad.Create(PlayCommand,StopCommand,RewindCommand);
Teclado.stop;
Teclado.play;
Teclado.rebobinar;
Teclado.Parar;
fim;
fim.