国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發(fā)設計 > 正文

Command模式

2019-11-18 18:22:12
字體:
來源:轉載
供稿:網(wǎng)友

最近學習模式入迷, 所以就想寫一篇關于模式的文章,這篇文章是<<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.
 


上一篇:只允許建立一次子窗體的MDI程序

下一篇:建立、讀取、存貯INI文件的方法《三》

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網(wǎng)友關注

主站蜘蛛池模板: 桃园县| 阿瓦提县| 慈溪市| 新巴尔虎左旗| 藁城市| 莱阳市| 离岛区| 乌拉特后旗| 广南县| 唐山市| 科技| 乐平市| 高阳县| 万载县| 九寨沟县| 曲沃县| 宁远县| 大英县| 凤庆县| 荔波县| 惠水县| 江口县| 彝良县| 临泽县| 垦利县| 阿鲁科尔沁旗| 玉山县| 凌海市| 江城| 富宁县| 瑞安市| 台中县| 武胜县| 安泽县| 当阳市| 绥德县| 贵定县| 浮梁县| 娄烦县| 迭部县| 安多县|