Delphi Command模式
更新時間:2008年03月15日 18:15:04 作者:
最近學(xué)習(xí)模式入迷, 所以就想寫一篇關(guān)于模式的文章,這篇文章是<<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對象向數(shù)據(jù)庫增加記錄哪一個更好
- 使用Jmail及Winwebmail發(fā)信時系統(tǒng)記錄中的錯誤:502 Error: command ...
- asp中command的在單條記錄時,有些字段顯示為空的問題
- javascript document.execCommand() 常用解析
- asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié)
- php設(shè)計模式 Command(命令模式)
- php設(shè)計模式 Command(命令模式)
- 獲取Repeter的Item和ItemIndex/CommandArgument實現(xiàn)思路與代碼
- 解決VS2012 Express的There was a problem sending the command to the program問題
- bash scp command not found的解決方法
- GridView中動態(tài)設(shè)置CommandField是否可用或可見的小例子
- document.execCommand()的用法小結(jié)
- pip 錯誤unused-command-line-argument-hard-error-in-future解決辦法
- 在RowCommand事件中獲取索引值示例代碼
- ON_COMMAND_RANGE多個按鈕響應(yīng)一個函數(shù)的解決方法
- C#命令模式(Command Pattern)實例教程
- ASP基礎(chǔ)知識Command對象講解
相關(guān)文章
Delphi XE5 為Android應(yīng)用制作簽名的方法(圖文)
這篇文章主要介紹了Delphi XE5 為Android應(yīng)用制作簽名的方法(圖文),需要的朋友可以參考下2016-02-02Delphi實現(xiàn)Listbox中的item根據(jù)內(nèi)容顯示不同顏色的方法
這篇文章主要介紹了Delphi實現(xiàn)Listbox中的item根據(jù)內(nèi)容顯示不同顏色的方法,需要的朋友可以參考下2014-07-07DELPHI7.0 獲取硬盤、CPU、網(wǎng)卡序列號的代碼
DELPHI7.0 獲取硬盤、CPU、網(wǎng)卡序列號的代碼,使用DELPHI編程的朋友可以參考下。2011-09-09截取指定符號之間的字符串(隨機(jī)讀取)delphi實例代碼
這篇文章主要介紹了截取指定符號之間的字符串(隨機(jī)讀取)delphi實例代碼,有需要的朋友可以參考一下2013-12-12Delphi實現(xiàn)檢測并枚舉系統(tǒng)安裝的打印機(jī)的方法
這篇文章主要介紹了Delphi實現(xiàn)檢測并枚舉系統(tǒng)安裝的打印機(jī)的方法,需要的朋友可以參考下2014-07-07