Design Pattern----02.Creational.Builder.Pattern (Delphi Sample)
2011-06-17 13:44
1021 查看
Intent
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.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.Check list
Decide if a common input and many possible representations (or outputs) is the problem at hand.Encapsulate the parsing of the common input in a Reader class.Design a standard protocol for creating all possible output representations. Capture the steps of this protocol in a Builder interface.Define a Builder derived class for each target representation.The client creates a Reader object and a Builder object, and registers the latter with the former.The client asks the Reader to “construct”.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, throughApplication.CreateFormand through the
TFormclass constructor.
TFormimplements 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
OnCreateevent also adds a hook into the builder process to make the functionality easy to extend.
Implementation Example
The following example includes a classTAbstractFormBuilderand two concrete classes
TRedFormBuilderand
TBlueFormBuilder. For ease of development some common functionality of the concrete classes has been moved into the shared
TAbstractFormBuilderclass.
1: type2: TAbstractFormBuilder = class[/code]
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[/code]
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)
UMLunit Pattern;interfaceusesClasses;typeTProduct = classprivateFParts: TStringList;publicconstructor 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)privateFProduct: TProduct;publicconstructor Create;procedure BuildPartA;procedure BuildPartB;function GetResult: TProduct;end;TBuilder2 = class(TInterfacedObject, IBuilder)privateFProduct: TProduct;publicconstructor Create;procedure BuildPartA;procedure BuildPartB;function GetResult: TProduct;end;TDirector = classpublicprocedure Construct(builder: IBuilder);end;implementation{ TProduct }procedure TProduct.Add(part: string);beginFParts.Add(part);end;constructor TProduct.Create;begininherited;FParts := TStringList.Create;end;destructor TProduct.Destroy;beginFParts.Free;inherited;end;procedure TProduct.Display;varI: integer;beginWriteLn('Product Patrs -----------');for I := 0 to FParts.Count - 1 do beginWriteLn(FParts[I]);end;end;{ TBuilder1 }procedure TBuilder1.BuildPartA;beginFProduct.Add('Part A ');end;procedure TBuilder1.BuildPartB;beginFProduct.Add('Part B ');end;constructor TBuilder1.Create;begininherited;FProduct := TProduct.Create;end;function TBuilder1.GetResult: TProduct;beginResult := FProduct;end;{ TBuilder2 }procedure TBuilder2.BuildPartA;beginFProduct.Add('Part X ');end;procedure TBuilder2.BuildPartB;beginFProduct.Add('Part Y ');end;constructor TBuilder2.Create;begininherited;FProduct := TProduct.Create;end;function TBuilder2.GetResult: TProduct;beginResult := FProduct;end;{ TDirector }procedure TDirector.Construct(builder: IBuilder);beginbuilder.BuildPartA;builder.BuildPartB;builder.BuildPartB;end;end.
-------------------------------------------------------------------------
program Creational.Builder.Pattern;{$APPTYPE CONSOLE}usesSysUtils,Pattern in 'Pattern.pas';varb1, b2: IBuilder;director: TDirector;product1, product2: TProduct;begin// ReportMemoryLeaksOnShutDown := DebugHook 0;trydirector := TDirector.Create;tryb1 := TBuilder1.Create;b2 := TBuilder2.Create;director.Construct(b1);product1 := b1.GetResult;product1.Display;director.Construct(b2);product2 := b2.GetResult;product2.Display;ReadLn;finallyproduct1.free;product2.free;director.Free;end;excepton E:Exception doWriteln(E.Classname, ': ', E.Message);end;end.
Download
Delphi Sourceaggregate ['æɡ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. 难懂的,复杂的相关文章推荐
- Design Pattern----03.Creational.FactoryMethod.Pattern (Delphi Sample)
- Design Pattern----01.Creational.AbstractFactory.Pattern (Delphi Sample)
- Design Pattern----24.Behavioral.State.Pattern (Delphi Sample)
- Design Pattern----04.Creational.Object Pool.Pattern (Java Sample)
- Design Pattern----25.Behavioral.Strategy.Pattern (Delphi Sample)
- Design Pattern----26.Behavioral.TemplateMethod.Pattern (Delphi Sample)
- Design Pattern----05.Creational.Prototype.Pattern (CSharp Sample)
- Design Pattern----27.Behavioral.Vistor.Pattern (Delphi Sample)
- Design Pattern----12.Structural.Flyweight.Pattern (Delphi Sample)
- Design Pattern----06.Creational.Singleton.Pattern (Delphi Sample)
- Design Pattern----09.Structural.Compsite.Pattern (Delphi Sample)
- Design Pattern----10.Structural.Decorator.Pattern (Delphi Sample)
- Design Pattern----18.Behavioral.Interpreter.Pattern (Delphi Sample)
- Design Pattern----19.Behavioral.Iterator.Pattern (Delphi Sample)
- Design Pattern----11.Structural.Facade.Pattern (Delphi Sample)
- Design Pattern----20.Behavioral.Mediator.Pattern (Delphi Sample)
- Design Pattern----21.Behavioral.Memento.Pattern (Delphi Sample)
- Design Pattern----23.Behavioral.Observer.Pattern (Delphi Sample)
- 【java类的设计模式—02】建造者模式(Builder pattern)
- Design Pattern----Creational.Pattern