依然是java 与模式里的例子,(java 与模式 闫宏著) 以下是转为delphi的代码,这次加了注释:)
unit BuilderPattern;
interface
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/) type
TProduct = class;
//抽像建造者 规范接口
TBuilder = class(TObject)
public
procedure builderpart1(); virtual; abstract;
procedure builderpart2(); virtual; abstract;
function returnProduct(): TProduct; virtual; abstract;
end;
//具体的创建者类,组件同他来创建,这样客户端不用知道具体的建造细节
(本文来源于图老师网站,更多请访问http://m.tulaoshi.com/bianchengyuyan/) TConcreteBuilder = class(TBuilder)
private
product: TProduct;
public
procedure builderpart1(); override;
procedure builderpart2(); override;
function returnProduct(): TProduct; override;
end;
//产品类
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 }
procedure TConcreteBuilder.builderpart1;
begin
// your code
end;
procedure TConcreteBuilder.builderpart2;
begin
// your code
end;
function TConcreteBuilder.returnProduct: TProduct;
begin
Result := product;
end;
end.