Delphi Command模式                
                
                    所属分类:
                        软件编程 / Delphi                    
                    阅读数:
                        86
                    
                        收藏 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实现保存和读取图片的方法,主要包括了显示图片、保存图片和读取图片的实现方法,是非常实用的技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
Delphi7中群发Email邮件的方法
这篇文章主要介绍了Delphi7中群发Email邮件的方法,涉及邮件服务器软件的使用,电子邮件的判断与发送功能的实现,是非常实用的技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
delphi字符串分隔函数用法实例
这篇文章主要介绍了delphi字符串分隔函数用法,通过自定义函数SeparateTerms2实现将字符串分割后存入字符串列表的功能,具有一定的实用价值,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
delphi实现将BMP格式图形转化为JPG格式图形的方法
这篇文章主要介绍了delphi实现将BMP格式图形转化为JPG格式图形的方法,通过简单的自定义函数调用系统自带的changefileext及SaveToFile等方法来实现格式转换功能,是非常实用的技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
Delphi解析FTP地址的方法
这篇文章主要介绍了Delphi解析FTP地址的方法,涉及Delphi解析FTP的相关技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
Delphi实现截屏存盘的方法
这篇文章主要介绍了Delphi实现截屏存盘的方法,涉及Delphi图片操作的相关技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
Delphi基本图像处理方法汇总
这篇文章主要介绍了Delphi基本图像处理方法,实例汇总了Delphi操作图像实现浮雕、反色、模糊、翻转等常用效果的方法,非常具有实用价值,需要的朋友可以参考下
                    
                    收藏 0赞 0分享
delphi制作wav文件的方法
这篇文章主要介绍了delphi制作wav文件的方法,涉及Delphi操作多媒体文件的相关技巧,需要的朋友可以参考下
                    
                    收藏 0赞 0分享