设计模式、用Delphi描述-->Abstract Factory模式

王朝delphi·作者佚名  2006-01-08
宽屏版  字体: |||超大  

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;

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

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有三个抽象的工厂方法CreateSpeedButton、CreateEdit、CreateLabel

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

下面是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.

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

Delphi实例

正在组织

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
© 2005- 王朝网络 版权所有