Delphi Command模式
更新時間:2008年03月15日 18:15:04 作者:
最近學習模式入迷, 所以就想寫一篇關于模式的文章,這篇文章是<<Java 與模式>> (閻宏 著)里的一個例子, 我把它改成Delphi的.第一次寫東西, 有不足之處希望大家可以諒解.
這個例子還是比較好理解的, 所以只給出代碼.
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.execute;
end;
procedure TKeyPad.rewind;
begin
RewindCommand.execute;
end;
procedure TKeyPad.stop;
begin
StopCommand.execute;
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.execute;
begin
inherited;
AudioPlayer.play();
end;
procedure TPlayCommand.Playcommand(AP: TAudioPlayer);
begin
self.AudioPlayer := AP;
end;
{ TRewindCommand }
procedure TRewindCommand.execute;
begin
inherited;
AudioPlayer.Rewind;
end;
procedure TRewindCommand.RewindCommand(AP: TAudioPlayer);
begin
AudioPlayer := ap;
end;
{ TStopCommand }
procedure TStopCommand.execute;
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.
您可能感興趣的文章:
- 用Command對象和RecordSet對象向數據庫增加記錄哪一個更好
- 使用Jmail及Winwebmail發(fā)信時系統(tǒng)記錄中的錯誤:502 Error: command ...
- asp中command的在單條記錄時,有些字段顯示為空的問題
- javascript document.execCommand() 常用解析
- asp.net gridview的Rowcommand命令中獲取行索引的方法總結
- php設計模式 Command(命令模式)
- php設計模式 Command(命令模式)
- 獲取Repeter的Item和ItemIndex/CommandArgument實現思路與代碼
- 解決VS2012 Express的There was a problem sending the command to the program問題
- bash scp command not found的解決方法
- GridView中動態(tài)設置CommandField是否可用或可見的小例子
- document.execCommand()的用法小結
- pip 錯誤unused-command-line-argument-hard-error-in-future解決辦法
- 在RowCommand事件中獲取索引值示例代碼
- ON_COMMAND_RANGE多個按鈕響應一個函數的解決方法
- C#命令模式(Command Pattern)實例教程
- ASP基礎知識Command對象講解
相關文章
Delphi XE5 為Android應用制作簽名的方法(圖文)
這篇文章主要介紹了Delphi XE5 為Android應用制作簽名的方法(圖文),需要的朋友可以參考下2016-02-02Delphi實現Listbox中的item根據內容顯示不同顏色的方法
這篇文章主要介紹了Delphi實現Listbox中的item根據內容顯示不同顏色的方法,需要的朋友可以參考下2014-07-07Delphi實現檢測并枚舉系統(tǒng)安裝的打印機的方法
這篇文章主要介紹了Delphi實現檢測并枚舉系統(tǒng)安裝的打印機的方法,需要的朋友可以參考下2014-07-07