Delphi Command模式
更新時(shí)間:2008年03月15日 18:15:04 作者:
最近學(xué)習(xí)模式入迷, 所以就想寫(xiě)一篇關(guān)于模式的文章,這篇文章是<<Java 與模式>> (閻宏 著)里的一個(gè)例子, 我把它改成Delphi的.第一次寫(xiě)東西, 有不足之處希望大家可以諒解.
這個(gè)例子還是比較好理解的, 所以只給出代碼.
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對(duì)象和RecordSet對(duì)象向數(shù)據(jù)庫(kù)增加記錄哪一個(gè)更好
- 使用Jmail及Winwebmail發(fā)信時(shí)系統(tǒng)記錄中的錯(cuò)誤:502 Error: command ...
- asp中command的在單條記錄時(shí),有些字段顯示為空的問(wèn)題
- javascript document.execCommand() 常用解析
- asp.net gridview的Rowcommand命令中獲取行索引的方法總結(jié)
- php設(shè)計(jì)模式 Command(命令模式)
- php設(shè)計(jì)模式 Command(命令模式)
- 獲取Repeter的Item和ItemIndex/CommandArgument實(shí)現(xiàn)思路與代碼
- 解決VS2012 Express的There was a problem sending the command to the program問(wèn)題
- bash scp command not found的解決方法
- GridView中動(dòng)態(tài)設(shè)置CommandField是否可用或可見(jiàn)的小例子
- document.execCommand()的用法小結(jié)
- pip 錯(cuò)誤unused-command-line-argument-hard-error-in-future解決辦法
- 在RowCommand事件中獲取索引值示例代碼
- ON_COMMAND_RANGE多個(gè)按鈕響應(yīng)一個(gè)函數(shù)的解決方法
- C#命令模式(Command Pattern)實(shí)例教程
- ASP基礎(chǔ)知識(shí)Command對(duì)象講解
相關(guān)文章
Delphi實(shí)現(xiàn)窗體感知鼠標(biāo)滑過(guò)并自動(dòng)隱藏與顯示窗口的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)窗體感知鼠標(biāo)滑過(guò)并自動(dòng)隱藏與顯示窗口的方法,涉及Delphi操作窗口及鼠標(biāo)事件的技巧,需要的朋友可以參考下2015-05-05
Delphi實(shí)現(xiàn)判斷網(wǎng)址是否存在及是否可以打開(kāi)的方法
這篇文章主要介紹了Delphi實(shí)現(xiàn)判斷網(wǎng)址是否存在及是否可以打開(kāi)的方法,需要的朋友可以參考下2014-07-07
delphi程序全屏顯示無(wú)標(biāo)題欄覆蓋整個(gè)屏幕(適合屏保)
delphi 簡(jiǎn)單實(shí)現(xiàn)程序全屏顯示無(wú)標(biāo)題欄,覆蓋整個(gè)屏幕,這個(gè)在做工控機(jī)或屏保時(shí)有用的,具體代碼如下,感興趣的朋友可以參考下哈2013-06-06
解決delphi TAdoQuery組件的close方法導(dǎo)致”列名無(wú)效“錯(cuò)誤的問(wèn)題
今天小編就為大家分享一篇解決delphi TAdoQuery組件的close方法導(dǎo)致”列名無(wú)效“錯(cuò)誤的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-02-02
delphi實(shí)現(xiàn)將BMP格式圖形轉(zhuǎn)化為JPG格式圖形的方法
這篇文章主要介紹了delphi實(shí)現(xiàn)將BMP格式圖形轉(zhuǎn)化為JPG格式圖形的方法,通過(guò)簡(jiǎn)單的自定義函數(shù)調(diào)用系統(tǒng)自帶的changefileext及SaveToFile等方法來(lái)實(shí)現(xiàn)格式轉(zhuǎn)換功能,是非常實(shí)用的技巧,需要的朋友可以參考下2014-12-12

