Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Builder pattern

From Wikipedia, the free encyclopedia
Design pattern in object-oriented programming

Thebuilder pattern is adesign pattern that provides a flexible solution to various object creation problems inobject-oriented programming. The builder patternseparates the construction of a complex object from its representation. It is one of the 23 classic design patterns described in the bookDesign Patterns and is sub-categorized as acreational pattern.[1]

Overview

[edit]

The builder design pattern solves problems like:[2]

  • How can a class (the same construction process) create different representations of a complex object?
  • How can a class that includes creating a complex object be simplified?

Creating and assembling the parts of a complex object directly within a class is inflexible. It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class.

The builder design pattern describes how to solve such problems:

  • Encapsulate creating and assembling the parts of a complex object in a separateBuilder object.
  • A class delegates object creation to aBuilder object instead of creating the objects directly.

A class (the same construction process) can delegate to differentBuilder objects to create different representations of a complex object.

Definition

[edit]

The intent of the builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.[1]

Advantages

[edit]

Advantages of the builder pattern include:[3]

  • Allows you to vary a product's internal representation.
  • Encapsulates code for construction and representation.
  • Provides control over the steps of the construction process.

Disadvantages

[edit]

Disadvantages of the builder pattern include:

  • A distinct ConcreteBuilder must be created for each type of product.[3]
  • Builder classes must be mutable.
  • May hamper/complicate dependency injection.
  • In manynull-safe languages, the builder pattern deferscompile-time errors for unset fields toruntime.

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the builder design pattern.[4]

In the aboveUMLclass diagram,theDirector class doesn't create and assemble theProductA1 andProductB1 objects directly.Instead, theDirector refers to theBuilder interface for building (creating and assembling) the parts of a complex object,which makes theDirector independent of which concrete classes are instantiated (which representation is created).TheBuilder1 class implements theBuilder interface by creating and assembling theProductA1 andProductB1 objects.
TheUMLsequence diagram shows the run-time interactions:TheDirector object callsbuildPartA() on theBuilder1 object, which creates and assembles theProductA1 object.Thereafter,theDirector callsbuildPartB() onBuilder1, which creates and assembles theProductB1 object.

Class diagram

[edit]
Builder Structure
Builder Structure
Builder
Abstract interface for creating objects (product).
ConcreteBuilder
Provides implementation for Builder. It is anobject able to construct other objects. Constructs and assembles parts to build the objects.

Examples

[edit]

AC# example:

namespaceWikipedia.Examples;/// <summary>/// Represents a product created by the builder./// </summary>publicclassBicycle{publicBicycle(stringmake,stringmodel,stringcolour,intheight){Make=make;Model=model;Colour=colour;Height=height;}publicstringMake{get;set;}publicstringModel{get;set;}publicintHeight{get;set;}publicstringColour{get;set;}}/// <summary>/// The builder abstraction./// </summary>publicinterfaceIBicycleBuilder{BicycleGetResult();stringColour{get;set;}intHeight{get;set;}}/// <summary>/// Concrete builder implementation./// </summary>publicclassGTBuilder:IBicycleBuilder{publicBicycleGetResult(){returnHeight==29?newBicycle("GT","Avalanche",Colour,Height):null;}publicstringColour{get;set;}publicintHeight{get;set;}}/// <summary>/// The director./// </summary>publicclassMountainBikeBuildDirector{privateIBicycleBuilder_builder;publicMountainBikeBuildDirector(IBicycleBuilderbuilder){_builder=builder;}publicvoidConstruct(){_builder.Colour="Red";_builder.Height=29;}publicBicycleGetResult(){returnthis._builder.GetResult();}}publicclassClient{publicvoidDoSomethingWithBicycles(){MountainBikeBuildDirectordirector=new(newGTBuilder());// Director controls the stepwise creation of product and returns the result.director.Construct();BicyclemyMountainBike=director.GetResult();}}

The Director assembles a bicycle instance in the example above, delegating the construction to a separate builder object that has been given to the Director by the Client.

See also

[edit]

Notes

[edit]
  1. ^abGamma et al. 1994, p. 97.
  2. ^"The Builder design pattern - Problem, Solution, and Applicability".w3sDesign.com. Retrieved2017-08-13.
  3. ^ab"Index of /archive/2010/winter/51023-1/presentations"(PDF).www.classes.cs.uchicago.edu. Retrieved2016-03-03.
  4. ^"The Builder design pattern - Structure and Collaboration".w3sDesign.com. Retrieved2017-08-12.

References

[edit]

External links

[edit]


Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Builder_pattern&oldid=1320950385"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp