设计模式、用Delphi描述-->Abstract Factory模式
2008-05-01 00:02
495 查看
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实例
正在组织相关文章推荐
- 设计模式、用Delphi描述-->Observer模式
- 设计模式、用Delphi描述-->Factory Method模式
- 设计模式、用Delphi描述-->Observer模式
- 设计模式、用Delphi描述-->Lock pattern
- 设计模式、用Delphi描述-->Visitor模式
- 设计模式、用Delphi描述-->Reference Count pattern
- 设计模式、用Delphi实现---->Template Method模式
- 设计模式、用Delphi描述-->Abstract Factory模式
- 设计模式、用Delphi实现---->Wrapper pattern
- 设计模式、用Delphi描述-->Factory Method模式
- 设计模式、用Delphi实现---->Strategy模式
- 所有的问题, 包括自己想象的问题 <<总结项目用到的设计模式>>
- Java笔记2 面向对象<2>static关键字、main函数、静态运用、单例设计模式
- <设计模式>系列汇总 By python and C++
- 设计模式 (python) ---> Singleton
- 设计模式、用Delphi实现---->Singleton 模式
- (Boolan)C++设计模式 <二> ——模版方法(Template Method)
- (Boolan)C++设计模式 <十二> ——命令模式(Command)和访问器(Visitor)
- JAVA设计模式(6) —<结构型>适配器模式(Adapter)
- 设计模式学习之抽象工厂模式(二)<反射+配置文件>