Template 模式之Delphi实现
2008-04-09 04:27:13来源:互联网 阅读 ()
unit Tpl_meth;
{
Design pattern example -
some simple classes using the Template Method pattern.
}
interface
type
TAbstractTemplateClass = class(TObject)
protected
function Algorithm_StepA: Integer; virtual; abstract;
function Algorithm_StepB: Integer; virtual; abstract;
function Algorithm_StepC: Integer; virtual; abstract;
public
function Algorithm: Integer;
end;
TConcreteClassA = class(TAbstractTemplateClass)
protected
function Algorithm_StepA: Integer; override;
function Algorithm_StepB: Integer; override;
function Algorithm_StepC: Integer; override;
end;
TConcreteClassB = class(TAbstractTemplateClass)
protected
function Algorithm_StepA: Integer; override;
function Algorithm_StepB: Integer; override;
function Algorithm_StepC: Integer; override;
end;
implementation
function TAbstractTemplateClass.Algorithm: Integer;
begin
Result := Algorithm_StepA
Algorithm_StepB
Algorithm_StepC;
end;
function TConcreteClassA.Algorithm_StepA: Integer;
begin
Result := 1;
end;
function TConcreteClassA.Algorithm_StepB: Integer;
begin
Result := 2;
end;
function TConcreteClassA.Algorithm_StepC: Integer;
begin
Result := 3;
end;
function TConcreteClassB.Algorithm_StepA: Integer;
begin
Result := 4;
end;
function TConcreteClassB.Algorithm_StepB: Integer;
begin
Result := 5;
end;
function TConcreteClassB.Algorithm_StepC: Integer;
begin
Result := 6;
end;
end.
/////////////////////////////////////////////////////////////////////////////////
unit Unit1;
interface
uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
Forms, Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
uses tpl_meth;
procedure TForm1.Button1Click(Sender: TObject);
var
AnAbstractObject: TAbstractTemplateClass;
begin
AnAbstractObject := TConcreteClassA.Create;
try
ShowMessage(''''Algorithm Result = '''' Inttostr(AnAbstractObject.Algorithm));
finally
AnAbstractObject.Destroy;
end;
end;
procedure TForm1.Button2Click(Sender: TObject);
var
AnAbstractObject: TAbstractTemplateClass;
begin
AnAbstractObject := TConcreteClassB.Create;
try
ShowMessage(''''Algorithm Result = '''' Inttostr(AnAbstractObject.Algorithm));
finally
AnAbstractObject.Destroy;
end;
end;
end.
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash