DesignPattern之FactoryMethod
来源:互联网 发布:梦里花落知多少知乎 编辑:程序博客网 时间:2024/05/16 05:19
unit Factory;
interface
uses Windows, Messages, SysUtils, Controls, Product;
type
IFactoryInterface = interface
function factory(): IProductInterface;
end;
TFactory1 = class(TControl, IFactoryInterface)
public
function factory(): IProductInterface;
end;
TFactory2 = class(TControl, IFactoryInterface)
public
function factory(): IProductInterface;
end;
implementation
function TFactory1.factory(): IProductInterface;
var
AProduct: TProduct1;
begin
AProduct := TProduct1.Create(nil);
Result := AProduct.CreateProduct();
end;
function TFactory2.factory(): IProductInterface;
var
AProduct: TProduct2;
begin
AProduct := TProduct2.Create(nil);
Result := AProduct.CreateProduct();
end;
end.
unit Product;
interface
uses Windows, Messages, SysUtils, Controls, Dialogs;
type
IProductInterface = interface
function CreateProduct(): IProductInterface;
end;
TProduct1 = class(TControl, IProductInterface)
public
function CreateProduct(): IProductInterface;
end;
TProduct2 = class(TControl, IProductInterface)
public
function CreateProduct(): IProductInterface;
end;
implementation
function TProduct1.CreateProduct(): IProductInterface;
begin
Result := Self;
showmessage('Product1 Created');
end;
function TProduct2.CreateProduct(): IProductInterface;
begin
Result := Self;
showmessage('Product2 Created');
end;
end.
//测试
procedure TForm1.Button1Click(Sender: TObject);
var
AFactory: TFactory1;
begin
AFactory := TFactory1.Create(Self);
AFactory.factory;
FreeAndNil(AFactory);
end;
procedure TForm1.Button2Click(Sender: TObject);
var
AFactory: TFactory2;
begin
AFactory := TFactory2.Create(Self);
AFactory.factory;
FreeAndNil(AFactory);
end;
- DesignPattern之FactoryMethod
- DesignPattern之SimpleFactory
- DesignPattern之Command
- DesignPattern之Bridge
- DesignPattern之Proxy
- DesignPattern之Mediator
- DesignPattern
- DesignPattern
- DesignPattern
- FactoryMethod
- factoryMethod
- 浅析设计模式之FactoryMethod
- 创建型模式之FactoryMethod
- 设计模式(DesignPattern)之策略模式(Strategy)
- 【设计模式系列】之Thinking In DesignPattern
- DesignPattern(设计模式) 之 单例模式
- 设计模式之C#实现--FactoryMethod
- 设计模式之C#实现FactoryMethod
- MicroBlaze Core Architecture
- 虚假中国银行(www.banochi.net)入侵尝试实录
- ASP.NET环境配置问题及解决方案
- 法国在世界舞台与美国对着干
- 进度条的演示。
- DesignPattern之FactoryMethod
- 我的简历
- 成功案例
- 五只毛毛虫的故事
- Nice用户手册(一)
- linux串口异步通信开发源代码
- 最酷简历
- [JavaScript]可以用在.NET中的JavaScript语句
- 桌面搜索是插入windows心脏的一把利剑