ITPub博客

首页 > Linux操作系统 > Linux操作系统 > Delphi设计模式-Abstract Factory (转)

Delphi设计模式-Abstract Factory (转)

原创 Linux操作系统 作者:worldblog 时间:2007-12-09 09:30:36 0 删除 编辑
Delphi设计模式-Abstract Factory (转)[@more@]

  模式的描述可以参考[1],下面举一个Delphi中的例子。

  通常Delphi中的Form都是自动Create,也可以在Form中写代码来创建(通常是在主Form中)。下面以Abstract Factory来创建Form。

//unit AbstractF

  TAbstractFactory = class
  public
  function CreateFoRM : TForm;virtual;abstract;
  end;

//unit ConcreteFA

  TConcreteFactoryA = class(TAbstractFactory)
  public
  function CreateForm : TForm; override;
  end;

{ TConcreteFactoryB }

function TConcreteFactoryA.CreateForm: TForm;
begin
  Result := TfrmFormA.Create(Application);
  Result.ShowModal;
end;

//unit ConcreteFB

  TConcreteFactoryB = class(TAbstractFactory)
  public
  function CreateForm : TForm; override;
  end;

{ TConcreteFactoryB }

function TConcreteFactoryB.CreateForm: TForm;
begin
  Result := TfrmFormB.Create(Application);
  Result.ShowModal;
end;

//主Form

unit MainFrm;

interface

uses
  windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, AbstractF;

type
  TfrmMain = class(TForm)
  btnConcreteA: TButton;
  btnContreteB: TButton;
  procedure btnConcreteAClick(Sender: Tobject);
  procedure btnContreteBClick(Sender: TObject);
  private
  { Private declarations }
  FAbstractFactory: TAbstractFactory;
  public
  { Public declarations }
  end;

var
  frmMain: TfrmMain;

implementation

uses ConcreteFA, ConcreteFB;

{$R *.dfm}

procedure TfrmMain.btnConcreteAClick(Sender: TObject);
begin
  FAbstractFactory:= TConcreteFactoryA.Create;
  FAbstractFactory.CreateForm;
  FAbstractFactory.Free;
end;

procedure TfrmMain.btnContreteBClick(Sender: TObject);
begin
  FAbstractFactory:= TConcreteFactoryB.Create;
  FAbstractFactory.CreateForm;
  FAbstractFactory.Free;
end;

end.

[参考资料]

1.《设计模式-可复用面向对象软件的基础》


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/10752043/viewspace-990256/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
  • 博文量
    6241
  • 访问量
    2448879