Builder pattern
Encyclopedia
The builder pattern is an object creation
software design pattern
. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects. Often, the builder pattern is used to build products in accordance with the composite pattern
.
Concrete Builder
Director
Product
Creational pattern
In software engineering, creational design patterns are design patterns that deal with object creation mechanisms, trying to create objects in a manner suitable to the situation. The basic form of object creation could result in design problems or added complexity to the design. Creational design...
software design pattern
Design pattern (computer science)
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into code. It is a description or template for how to solve a problem that...
. The intention is to abstract steps of construction of objects so that different implementations of these steps can construct different representations of objects. Often, the builder pattern is used to build products in accordance with the composite pattern
Composite pattern
In software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent...
.
Definition
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.Structure
Builder- Abstract interface for creating objects (product).
Concrete Builder
- Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.
Director
- The Director class is responsible for managing the correct sequence of object creation. It receives a Concrete Builder as a parameter and executes the necessary operations on it.
Product
- The final object that will be created by the Director using Builder.
Useful tips
- Builder focuses on constructing a complex object step by step. Abstract FactoryAbstract factory patternThe abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the...
emphasizes a family of product objects (either simple or complex). Builder returns the product as a final step, but as far as the Abstract FactoryAbstract factory patternThe abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the...
is concerned, the product gets returned immediately. - Builder often builds a CompositeComposite patternIn software engineering, the composite pattern is a partitioning design pattern. The composite pattern describes that a group of objects are to be treated in the same way as a single instance of an object. The intent of a composite is to "compose" objects into tree structures to represent...
. - Often, designs start out using Factory MethodFactory method patternThe factory method pattern is an object-oriented design pattern to implement the concept of factories. Like other creational patterns, it deals with the problem of creating objects without specifying the exact class of object that will be created.The creation of an object often requires complex...
(less complicated, more customizable, subclasses proliferate) and evolve toward Abstract FactoryAbstract factory patternThe abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the...
, PrototypePrototype patternThe prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects...
, or Builder (more flexible, more complex) as the designer discovers where more flexibility is needed. - Sometimes creational patterns are complementary: Builder can use one of the other patterns to implement which components are built. Abstract FactoryAbstract factory patternThe abstract factory pattern is a software design pattern that provides a way to encapsulate a group of individual factories that have a common theme. In normal usage, the client software creates a concrete implementation of the abstract factory and then uses the generic interfaces to create the...
, Builder, and PrototypePrototype patternThe prototype pattern is a creational design pattern used in software development when the type of objects to create is determined by a prototypical instance, which is cloned to produce new objects...
can use SingletonSingleton patternIn software engineering, the singleton pattern is a design pattern used to implement the mathematical concept of a singleton, by restricting the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system...
in their implementations. - Builders are good candidates for a fluent interfaceFluent interfaceIn software engineering, a fluent interface is an implementation of an object oriented API that aims to provide for more readable code....
.
C#
Java
C++
External links
- The JavaWorld article Build user interfaces without getters and setters (Allen HolubAllen HolubAllen Holub is a computer scientist, author, educator, and consultant. He has written extensively on the C, C++, and Java programming languages, and on object-oriented programming in general...
) shows the complete Java source code for a Builder. - Item 2: Consider a builder by Joshua Bloch