Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1068408
  • 博文数量: 282
  • 博客积分: 10865
  • 博客等级: 上将
  • 技术积分: 2480
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-12 12:35
文章存档

2017年(1)

2016年(3)

2015年(10)

2014年(12)

2013年(5)

2012年(10)

2011年(29)

2010年(3)

2008年(13)

2007年(92)

2006年(104)

我的朋友

分类: 项目管理

2007-09-20 23:35:14

Abstract Factory模式(抽象工厂)

起源

Delphi中的Abstract Factory模式在基本Abstract Factory模式进行了扩展。更多Abstract Factory模式的资料请参阅 [Gam+]

目的

提供一个创建一系列相关或互依赖对象的接口,面无需指定它们的具体的类。

动机

这种模式是将你的应用与具体的类分类的最好办法,比如说,你要覆盖Delphi的公正的VCL

你可以创建一个抽象工厂来实现自己的要组件。

应用

下面的例子使用一个抽象类工厂和两个实际的类工厂来实现不同特色的用户界面组件。TOAbstractFactory是一个单一的组件单独的类,以每个产品系列通常只需要个工厂。

 

TOAbstractFactory = class(TObject)

  public

    constructor Create;

    destructor Destroy; override;

    { 抽象的构造}

    function CreateSpeedButton(AOwner: TComponent): TSpeedButton; virtual; abstract;

    function CreateEdit(AOwner: TComponent): TEdit; virtual; abstract;

    function CreateLabel(AOwner: TComponent): TLabel; virtual; abstract;

  end;

 

TORedFactoryTOBlueFactory重载了抽象的接口,用来支持装饰不同的特色界面。

 

TORedFactory = class(TOAbstractFactory)

  public

    {红色构造器 }

    function CreateSpeedButton(AOwner: TComponent): TSpeedButton; override;

    function CreateEdit(AOwner: TComponent): TEdit; override;

    function CreateLabel(AOwner: TComponent): TLabel; override;

  end;

 

TOBlueFactory = class(TOAbstractFactory)

  public

    {蓝色构造器}

    function CreateSpeedButton(AOwner: TComponent): TSpeedButton; override;

    function CreateEdit(AOwner: TComponent): TEdit; override;

    function CreateLabel(AOwner: TComponent): TLabel; override;

  end;

有上面的接口中:

·      声明了一个创建抽象产品对象的接口:TOAbstractFactory

¨    TOAbstractFactory有三个抽象的工厂方法CreateSpeedButtonCreateEditCreateLabel

·      TORedFactoryTOBlueFactory用来实现创建具体产品对象的方法

下面是TORedFactory的实现代码:

·     unit Redfact;

·     interface

·     uses

·       Classes, SysUtils, StdCtrls, Graphics, Abstfact, Buttons;

·     type

·       TORedFactory = class(TOAbstractFactory)

·       public

·         { 实际的构造器}

·         function CreateSpeedButton(AOwner: TComponent): TSpeedButton; override;

·         function CreateEdit(AOwner: TComponent): TEdit; override;

·         function CreateLabel(AOwner: TComponent): TLabel; override;

·       end;

·      

·     implementation

·     {具体的构造,被工厂方法隐藏}

·     type

·       TRedSpeedButton = class(TSpeedButton)

·       public

·         constructor Create(AOwner: TComponent); override;

·       end;

·      

·       TRedEdit = class(TEdit)

·       public

·         constructor Create(AOwner: TComponent); override;

·       end;

·      

·       TRedLabel = class(TLabel)

·       public

·         constructor Create(AOwner: TComponent); override;

·       end;

·      

·     { widget implementation }

·      

·     { TRedButton }

·      

·     constructor TRedSpeedButton.Create(AOwner: TComponent);

·     begin

·       inherited Create(AOwner);

·       Font.Color := clRed;

·     end;

·      

·     { TRedEdit }

·      

·     constructor TRedEdit.Create(AOwner: TComponent);

·     begin

·       inherited Create(AOwner);

·       Font.Color := clRed;

·     end;

·      

·     { TRedLabel }

·      

·     constructor TRedLabel.Create(AOwner: TComponent);

·     begin

·       inherited Create(AOwner);

·       Font.Color := clRed;

·     end;

·      

·      

·     { the concrete factory }

·      

·     function TORedFactory.CreateSpeedButton(AOwner: TComponent): TSpeedButton;

·     begin

·       Result := TRedSpeedButton.Create(AOwner);

·     end;

·      

·     function TORedFactory.CreateEdit(AOwner: TComponent): TEdit;

·     begin

·       Result := TRedEdit.Create(AOwner);

·     end;

·      

·     function TORedFactory.CreateLabel(AOwner: TComponent): TLabel;

·     begin

·       Result := TRedLabel.Create(AOwner);

·     end;

end.

 

运行的时候,我们的客户程序的实例由类的工厂具体类创建。并且客户程序不需知道使用工厂的具体子类。

 

 

阅读(1867) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~