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...
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 Factory
Abstract factory pattern
The 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 Factory
Abstract factory pattern
The 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.
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...
The 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 Factory
Abstract factory pattern
The 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...
The 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 Factory
Abstract factory pattern
The 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...
The 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...
In 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 software engineering, a fluent interface is an implementation of an object oriented API that aims to provide for more readable code....
.
C#
using System;
namespace BuilderPattern
{
// Builder - abstract interface for creating objects (the product, in this case)
abstract class PizzaBuilder
{
protected Pizza pizza;
public Pizza GetPizza
{
return pizza;
}
public void CreateNewPizzaProduct
{
pizza = new Pizza;
}
public abstract void BuildDough;
public abstract void BuildSauce;
public abstract void BuildTopping;
}
// Concrete Builder - provides implementation for Builder; an object able to construct other objects.
// Constructs and assembles parts to build the objects
class HawaiianPizzaBuilder : PizzaBuilder
{
public override void BuildDough
{
pizza.Dough = "cross";
}
public override void BuildSauce
{
pizza.Sauce = "mild";
}
public override void BuildTopping
{
pizza.Topping = "ham+pineapple";
}
}
// Concrete Builder - provides implementation for Builder; an object able to construct other objects.
// Constructs and assembles parts to build the objects
class SpicyPizzaBuilder : PizzaBuilder
{
public override void BuildDough
{
pizza.Dough = "pan baked";
}
public override void BuildSauce
{
pizza.Sauce = "hot";
}
// Director - responsible for managing the correct sequence of object creation.
// . receives a Concrete Builder as a parameter and executes the necessary operations on it
class Cook
{
private PizzaBuilder _pizzaBuilder;
public void SetPizzaBuilder(PizzaBuilder pb)
{
_pizzaBuilder = pb;
}
public Pizza GetPizza
{
return _pizzaBuilder.GetPizza;
}
public void ConstructPizza
{
_pizzaBuilder.CreateNewPizzaProduct;
_pizzaBuilder.BuildDough;
_pizzaBuilder.BuildSauce;
_pizzaBuilder.BuildTopping;
}
}
// Product - The final object that will be created by the Director using Builder
public class Pizza
{
private string _dough = string.Empty;
private string _sauce = string.Empty;
private string _topping = string.Empty;
public string Dough
{
get { return _dough; }
set { _dough = value; }
}
public string Sauce
{
get { return _sauce; }
set { _sauce = value; }
}
public string Topping
{
get { return _topping; }
set { _topping = value; }
}
}
class Program
{
static void Main(string[] args)
{
PizzaBuilder hawaiianPizzaBuilder = new HawaiianPizzaBuilder;
Cook cook = new Cook;
cook.SetPizzaBuilder(hawaiianPizzaBuilder);
cook.ConstructPizza;
// create the product
Pizza hawaiian = cook.GetPizza;
PizzaBuilder spicyPizzaBuilder = new SpicyPizzaBuilder;
cook.SetPizzaBuilder(spicyPizzaBuilder);
cook.ConstructPizza;
// create another product
Pizza spicy = cook.GetPizza;
}
}
}
Java
public class Pizza {
private final String dough;
private final String sauce;
private final String topping;
public String getDough {
return dough;
}
public String getSauce {
return sauce;
}
public String getTopping {
return topping;
}
Allen 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.