今天图老师小编给大家精心推荐个建造者模式-Delphi述教程,一起来看看过程究竟如何进行吧!喜欢还请点个赞哦~
【 tulaoshi.com - 编程语言 】
依然是java 与模式里的例子,(java 与模式 闫宏著) 以下是转为delphi的代码,这次加了注释:)
unit BuilderPattern;
interface
type
TProduct = class;
//抽像建造者 规范接口
TBuilder = class(TObject)
public
procedure builderpart1(); virtual; abstract;
procedure builderpart2(); virtual; abstract;
function returnProduct(): TProduct; virtual; abstract;
end;
//具体的创建者类,组件同他来创建,这样客户端不用知道具体的建造细节
TConcreteBuilder = class(TBuilder)
private
product: TProduct;
public
procedure builderpart1(); override;
procedure builderpart2(); override;
function returnProduct(): TProduct; override;
end;
//产品类
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/) TProduct = class(TObject)
//
end;
//导演者角色,直接被客户端使用
TDirector = class(TObject)
private
Builder: TBuilder;
public
procedure Initialize();
end;
implementation
{ TDirector }
procedure TDirector.Initialize;
begin
Builder := TConcreteBuilder.Create;
Builder.builderpart1();
Builder.builderpart2();
Builder.returnProduct;
end;
{ TConcreteBuilder }
(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/) procedure TConcreteBuilder.builderpart1;
begin
// your code
end;
procedure TConcreteBuilder.builderpart2;
begin
// your code
end;
function TConcreteBuilder.returnProduct: TProduct;
begin
Result := product;
end;
end.
来源:http://www.tulaoshi.com/n/20160219/1601325.html
看过《建造者模式-Delphi述》的人还看了以下文章 更多>>