Delphi Command模式

所属分类: 软件编程 / Delphi 阅读数: 118
收藏 0 赞 0 分享

这个例子还是比较好理解的, 所以只给出代码.
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.
 
更多精彩内容其他人还在看

Delphi基本图像处理方法汇总

这篇文章主要介绍了Delphi基本图像处理方法,实例汇总了Delphi操作图像实现浮雕、反色、模糊、翻转等常用效果的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享

Windows API GetLastError()函数返回值含义解释

这篇文章主要介绍了Windows API GetLastError()函数返回值含义解释,本文罗列了所有错误代码及中文注释,需要的朋友可以参考下
收藏 0 赞 0 分享

delphi制作wav文件的方法

这篇文章主要介绍了delphi制作wav文件的方法,涉及Delphi操作多媒体文件的相关技巧,需要的朋友可以参考下
收藏 0 赞 0 分享

ListView 百分比进度条(delphi版)

本文通过实例代码给大家介绍ListView 百分比进度条,本文使用的是delphi语言实现的,代码比较简单实用,希望的朋友参考下
收藏 0 赞 0 分享

Delphi XE5 为Android应用制作签名的方法(图文)

这篇文章主要介绍了Delphi XE5 为Android应用制作签名的方法(图文),需要的朋友可以参考下
收藏 0 赞 0 分享

TImage组件实现保存图片到Stream

这篇文章主要介绍了TImage组件实现保存图片到Stream以及从stream中读取图片的方法,非常的实用,有需要的小伙伴可以参考下
收藏 0 赞 0 分享

Delphi提取PDF文本实例

下面小编就为大家带来一篇Delphi提取PDF文本实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
收藏 0 赞 0 分享

Delphi 中内存映射对于大文件的使用

这篇文章主要介绍了Delphi 中内存映射对于大文件的使用的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi 根据字符串找到函数并执行的实例

这篇文章主要介绍了Delphi 根据字符串找到函数并执行的实例的相关资料,希望通过本能帮助到大家实现这样的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

Delphi 用DLL实现插件的简单实例

这篇文章主要介绍了Delphi 用DLL实现插件的简单实例的相关资料,希望通过本文能帮助到大家,这里提供实例帮助大家掌握这部分内容,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多