建造者模式-Delphi述

类别:Delphi 点击:0 评论:0 推荐:

依然是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;

  //产品类

  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.

本文地址:http://com.8s8s.com/it/it5046.htm