在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
ProblemAn 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. DiscussionSeparate 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”. StructureThe Reader encapsulates the parsing of the common input. The Builder hierarchy makes possible the polymorphic creation of many peculiar representations or targets. ExampleThe 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
Rules of thumb
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 Implementation ExampleThe following example includes a class
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:
Builder in Delphi(Two)UML 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. Downloadaggregate ['æɡ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. 难懂的,复杂的 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论