The builder pattern is a design pattern designed to provide a flexible solution to various object creation problems in object-oriented programming. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.
The Builder design pattern is one of the GoF design patterns[1] that describe how to solve recurring design problems in object-oriented software.
The Builder design pattern solves problems like:[2]
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:
Builder
object.Builder
object instead of creating the objects directly.A class (the same construction process) can delegate to different Builder
objects to create different representations of a complex object.
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 of the Builder pattern include:[3]
Disadvantages of the Builder pattern include:[3]
In the above UML class diagram,
the Director
class doesn't create and assemble the ProductA1
and ProductB1
objects directly.
Instead, the Director
refers to the Builder
interface for building (creating and assembling) the parts of a complex object,
which makes the Director
independent of which concrete classes are instantiated (which representation is created).
The Builder1
class implements the Builder
interface by creating and assembling the ProductA1
and ProductB1
objects.
The UML sequence diagram shows the run-time interactions:
The Director
object calls buildPartA()
on the Builder1
object, which creates and assembles the ProductA1
object.
Thereafter,
the Director
calls buildPartB()
on Builder1
, which creates and assembles the ProductB1
object.
/// <summary>
/// Represents a product created by the builder
/// </summary>
public class Bicycle
{
public string Make { get; set; }
public string Model { get; set; }
public int Height { get; set; }
public string Colour { get; set; }
public Bicycle(string make, string model, string colour, int height)
{
Make = make;
Model = model;
Colour = colour;
Height = height;
}
}
/// <summary>
/// The builder abstraction
/// </summary>
public interface IBicycleBuilder
{
string Colour { get; set; }
int Height { get; set; }
Bicycle GetResult();
}
/// <summary>
/// Concrete builder implementation
/// </summary>
public class GTBuilder : IBicycleBuilder
{
public string Colour { get; set; }
public int Height { get; set; }
public Bicycle GetResult()
{
return Height == 29 ? new Bicycle("GT", "Avalanche", Colour, Height) : null;
}
}
/// <summary>
/// The director
/// </summary>
public class MountainBikeBuildDirector
{
private IBicycleBuilder _builder;
public MountainBikeBuildDirector(IBicycleBuilder builder)
{
_builder = builder;
}
public void Construct()
{
_builder.Colour = "Red";
_builder.Height = 29;
}
}
public class Client
{
public void DoSomethingWithBicycles()
{
var builder = new GTBuilder();
var director = new MountainBikeBuildDirector(builder);
director.Construct();
Bicycle myMountainBike = builder.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.
By: Wikipedia.org
Edited: 2021-06-18 19:28:45
Source: Wikipedia.org