Design Pattern----02.Creational.Builder.Pattern ,Delphi Sample

  • Separate the construction of a complex object from its representation so that the same construction process can create different representations.
  • Parse a complex representation, create one of several targets.

Problem

An application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

Discussion

Separate the algorithm for interpreting (i.e. reading and parsing) a stored persistence mechanism (e.g. RTF files) from the algorithm for building and representing one of many target products (e.g. ASCII, TeX, text widget). The focus/distinction is on creating complex aggregates.

The “director” invokes “builder” services as it interprets the external format. The “builder” creates part of the complex object each time it is called and maintains all intermediate state. When the product is finished, the client retrieves the result from the “builder”.

Affords finer control over the construction process. Unlike creational patterns that construct products in one shot, the Builder pattern constructs the product step by step under the control of the “director”.

Structure

The Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets.

Design Pattern----02.Creational.Builder.Pattern ,Delphi Sample

Example

The Builder pattern separates the construction of a complex object from its representation so that the same construction process can create different representations. This pattern is used by fast food restaurants to construct children’s meals. Children’s meals typically consist of a main item, a side item, a drink, and a toy (e.g., a hamburger, fries, Coke, and toy dinosaur). Note that there can be variation in the content of the children’s meal, but the construction process is the same. Whether a customer orders a hamburger, cheeseburger, or chicken, the process is the same. The employee at the counter directs the crew to assemble a main item, side item, and toy. These items are then placed in a bag. The drink is placed in a cup and remains outside of the bag. This same process is used at competing restaurants.

Design Pattern----02.Creational.Builder.Pattern ,Delphi Sample

Check list

  1. Decide if a common input and many possible representations (or outputs) is the problem at hand.
  2. Encapsulate the parsing of the common input in a Reader class.
  3. Design a standard protocol for creating all possible output representations. Capture the steps of this protocol in a Builder interface.
  4. Define a Builder derived class for each target representation.
  5. The client creates a Reader object and a Builder object, and registers the latter with the former.
  6. The client asks the Reader to “construct”.
  7. The client asks the Builder to return the result.

Rules of thumb

  • Sometimes creational patterns are complementory: Builder can use one of the other patterns to implement which components get built. Abstract Factory, Builder, and Prototype can use Singleton in their implementations.
  • Builder focuses on constructing a complex object step by step. Abstract Factory emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract Factory is concerned, the product gets returned immediately.
  • Builder often builds a Composite.
  • Often, designs start out using Factory Method (less complicated, more customizable, subclasses proliferate) and evolve toward Abstract Factory, Prototype, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed.

Applications in Delphi(One)

The functionality used in Delphi’s VCL to create forms and components is similar in concept to the builder. Delphi creates forms using a common interface, through Application.CreateForm and through the TForm class constructor. TForm implements a common constructor using the resource information (DFM file) to instantiate the components owned by the form. Many descendant classes reuse this same construction process to create different representations. Delphi also makes developer extensions easy. TForm’s OnCreate event also adds a hook into the builder process to make the functionality easy to extend.

Implementation Example

The following example includes a class TAbstractFormBuilder and two concrete classes TRedFormBuilder and TBlueFormBuilder. For ease of development some common functionality of the concrete classes has been moved into the shared TAbstractFormBuilder class.

  1: type
  2:   TAbstractFormBuilder = class
  3:   private
  4:     FForm: TForm;
  5:     procedure BuilderFormClose(Sender: TObject; var Action: TCloseAction);
  6:   protected
  7:     function GetForm: TForm; virtual;
  8:   public
  9:     procedure CreateForm(AOwner: TComponent); virtual;
 10:     procedure CreateSpeedButton; virtual; abstract;
 11:     procedure CreateEdit; virtual; abstract;
 12:     procedure CreateLabel; virtual; abstract;
 13:     property Form: TForm read GetForm;
 14:   end;
 15: 
 16: type
 17:   TRedFormBuilder = class(TAbstractFormBuilder)
 18:   private
 19:     FNextLeft, FNextTop: Integer;
 20:   public
 21:     procedure CreateForm(AOwner: TComponent); override;
 22:     procedure CreateSpeedButton; override;
 23:     procedure CreateEdit; override;
 24:     procedure CreateLabel; override;
 25:   end;
 26: 
 27: type
 28:   TBlueFormBuilder = class(TAbstractFormBuilder)
 29:   private
 30:     FNextLeft, FNextTop: Integer;
 31:   public
 32:     procedure CreateForm(AOwner: TComponent); override;
 33:     procedure CreateSpeedButton; override;
 34:     procedure CreateEdit; override;
 35:     procedure CreateLabel; override;
 36:   end;

At runtime the client application instructs one of the concrete classes to create parts using the public part creation procedures. The concrete builder instance is passed to the folliwing procedure:

  1: procedure TForm1.Create3ComponentFormUsingBuilder(ABuilder: TAbstractFormBuilder);
  2: var
  3:   NewForm: TForm;
  4: begin
  5:   with ABuilder do begin
  6:     CreateForm(Application);
  7:     CreateEdit;
  8:     CreateSpeedButton;
  9:     CreateLabel;
 10:     NewForm := Form;
 11:     if NewForm <> nil then NewForm.Show;
 12:   end;
 13: end;

Builder in Delphi(Two)

UML

Design Pattern----02.Creational.Builder.Pattern ,Delphi Sample

unit Pattern;

interface

uses
  Classes;

type

  TProduct = class
  private
    FParts: TStringList;
  public
    constructor Create;
    destructor Destroy; override;
    procedure Add(part: string);
    procedure Display;
  end;

  IBuilder = interface
  ['{9769DCD1-A2F5-4105-B28D-8A34DA6B0C12}']
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TBuilder1 = class(TInterfacedObject, IBuilder)
  private
    FProduct: TProduct;
  public
    constructor Create;
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TBuilder2 = class(TInterfacedObject, IBuilder)
  private
    FProduct: TProduct;
  public
    constructor Create;
    procedure BuildPartA;
    procedure BuildPartB;
    function GetResult: TProduct;
  end;

  TDirector = class
  public
    procedure Construct(builder: IBuilder);
  end;

implementation

{ TProduct }

procedure TProduct.Add(part: string);
begin
  FParts.Add(part);
end;

constructor TProduct.Create;
begin
  inherited;
  FParts := TStringList.Create;
end;

destructor TProduct.Destroy;
begin
  FParts.Free;
  inherited;
end;

procedure TProduct.Display;
var
  I: integer;
begin
  WriteLn('Product Patrs -----------');
  for I := 0 to FParts.Count - 1 do begin
    WriteLn(FParts[I]);
  end;
end;

{ TBuilder1 }

procedure TBuilder1.BuildPartA;
begin
  FProduct.Add('Part A ');
end;

procedure TBuilder1.BuildPartB;
begin
  FProduct.Add('Part B ');
end;

constructor TBuilder1.Create;
begin
  inherited;
  FProduct := TProduct.Create;
end;

function TBuilder1.GetResult: TProduct;
begin
  Result := FProduct;
end;

{ TBuilder2 }

procedure TBuilder2.BuildPartA;
begin
  FProduct.Add('Part X ');
end;

procedure TBuilder2.BuildPartB;
begin
  FProduct.Add('Part Y ');
end;

constructor TBuilder2.Create;
begin
  inherited;
  FProduct := TProduct.Create;
end;

function TBuilder2.GetResult: TProduct;
begin
  Result := FProduct;
end;

{ TDirector }

procedure TDirector.Construct(builder: IBuilder);
begin
  builder.BuildPartA;
  builder.BuildPartB;
  builder.BuildPartB;
end;

end.
-------------------------------------------------------------------------
program Creational.Builder.Pattern;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Pattern in 'Pattern.pas';

var
  b1, b2: IBuilder;
  director: TDirector;
  product1, product2: TProduct;

begin
//  ReportMemoryLeaksOnShutDown := DebugHook  0;
  try
    director := TDirector.Create;
    try
      b1 := TBuilder1.Create;
      b2 := TBuilder2.Create;

      director.Construct(b1);
      product1 := b1.GetResult;
      product1.Display;

      director.Construct(b2);
      product2 := b2.GetResult;
      product2.Display;

      ReadLn;
    finally
      product1.free;
      product2.free;
      director.Free;
    end;
  except
    on E:Exception do
      Writeln(E.Classname, ': ', E.Message);
  end;
end.

Download

Delphi Source

aggregate ['æɡriɡət, 'æɡriɡeit]

· vi. 集合;聚集;合计

· vt. 集合;聚集;合计

· n. 合计;集合体;总计

· adj. 聚合的;集合的;合计的

polymorphic [,pɔli'mɔ:fik]

· adj. [生物] 多态的;[生物] 多形的;多形态的;[化学] 多晶形的(等于 polymorphous)

acid ['æsid]

· n. 酸;<俚>迷幻药

· adj. 酸的;讽刺的;刻薄的

peculiar [pi'kju:ljə]

· adj. 特殊的;独特的;奇怪的;罕见的

· n. 特权;特有财产

emphasize ['emfəsaiz]

· vt. 强调,着重

complicated ['kɔmplikeitid]

· adj. 难懂的,复杂的