Builder Pattern

The Builder pattern is a creation pattern that separates the construction of a complex object from its representation so that the same construction process can create different representations.

The Builder pattern uses multiple simple objects to build a complex object step by step.

role

1. Abstract Builder

Give an abstract conclusion to regulate the construction of the various components of the product object. This interface specifies which parts of a complex object to implement and does not involve the creation of specific object parts;

2. Concrete Builder

Implements the builder interface, which concretes the creation of parts of complex objects for different business logic. Provide examples of products after the construction process is completed;

3. The Director

Invoke specific builders to create parts of a complex object, with no product-specific information involved in the mentor, only responsible for ensuring that the parts of the object are created intact or in a certain order;

4. Product

Specific complex objects to create.

The sample





The BuilderPattern namespace contains Vehicle as product class, Builder base class as abstract Builder, Dongfeng Yuedakia class, Volkswagen class and Tesla Tesla class. Also includes 1 motor vehicle extension class and 1 instructor. This example demonstrates the strategy used by three different vehicle manufacturers to produce more complex vehicles.

public abstract class Builder {

    protected Vehicle _product = new Vehicle();

    public abstract void BuildCarframe();

    public abstract void BuildWheel();

    public abstract void BuildDoor();

    public abstract void BuildApparatus();

    public abstract void BuildColor();

    public virtual Vehicle GetResult() {
        return_product; }}Copy the code

The Builder base class Builder defines the interface to produce different parts of the car and maintains references to the vehicle internally.

public class Tesla : Builder {

    public override void BuildCarframe() {
        _product.Carframe = "ZZ32093M3485C1356";
    }

    public override void BuildWheel() {
        _product.Wheel = 4;
    }

    public override void BuildDoor() {
        _product.Door = 5;
    }

    public override void BuildApparatus() {
        _product.Apparatus = "Model X";
    }

    public override void BuildColor(){ _product.Color = Color.Red; }}Copy the code

Concrete Builder Tesla class, the implementation of the abstract Builder Builder interface.

public class Volkswagen : Builder {

    public override void BuildCarframe() {
        _product.Carframe = "VS89362P1903C9852";
    }

    public override void BuildWheel() {
        _product.Wheel = 4;
    }

    public override void BuildDoor() {
        _product.Door = 4;
    }

    public override void BuildApparatus() {
        _product.Apparatus = "Skoda";
    }

    public override void BuildColor(){ _product.Color = Color.Blue; }}Copy the code

Concrete Builder Volkswagen class, to achieve the interface of abstract Builder Builder.

public class Yuedakia : Builder {

    public override void BuildCarframe() {
        _product.Carframe = "YK9391J0231L30D32";
    }

    public override void BuildWheel() {
        _product.Wheel = 4;
    }

    public override void BuildDoor() {
        _product.Door = 4;
    }

    public override void BuildApparatus() {
        _product.Apparatus = "Kia K3";
    }

    public override void BuildColor(){ _product.Color = Color.Wheat; }}Copy the code

Concrete Builder Dongfeng Yueda Yuedakia class, to achieve the interface of abstract Builder Builder.

public class Vehicle {
 
    public string Carframe { get; set; }
 
    public int Wheel { get; set; }
 
    public int Door { get; set; }
 
    public string Apparatus { get; set; }
 
    public Color Color { get; set; }
 
    public void Print() {
        Console.WriteLine($"{this.VehicleInfo()}"); }}Copy the code

The Vehicle class, which is the complex object we want to create with the Builder pattern.

public class Director {

    public void Construct(Builder builder){ builder.BuildCarframe(); builder.BuildWheel(); builder.BuildDoor(); builder.BuildApparatus(); builder.BuildColor(); }}Copy the code

This is the guidance class Director, which is responsible for the object creation process.

public static class Extentions {
 
    public static string VehicleInfo(this Vehicle vehicle) {
        var type = vehicle.GetType();
        var properties = type.GetProperties();
        var result = string.Empty;
        foreach (var prop in properties) {
            result +=
                $"{prop.Name}:{prop.GetValue(vehicle, null)}{Environment.NewLine}";
        }
        returnresult; }}Copy the code

An extension class that makes it easy to print out related information during presentations.

public class Program {

    private static Director _director = null;

    private static List<Builder> _builders = null;

    private static Vehicle _vehicle = null;

    public static void Main(string[] args) {
        _director = new Director();
        _vehicle = new Vehicle();

        _builders = new List<Builder>() {
            new Tesla(),
            new Volkswagen(),
            new Yuedakia()
        };

        foreach (var builder in_builders) { _director.Construct(builder); _vehicle = builder.GetResult(); _vehicle.Print(); } Console.ReadKey(); }}Copy the code

This is the caller’s code, which maintains references to the mentor, builder, and vehicle, respectively. Next we create three cars, Tesla’s Model X, Volkswagen’s Skoda, and Dongfeng Yueda’s Kia K3. Here is the output of this case:

Carframe:ZZ32093M3485C1356
Wheel:4
Door:5
Apparatus:Model X
Color:Color [Red]

Carframe:VS89362P1903C9852
Wheel:4
Door:4
Apparatus:Skoda
Color:Color [Blue]

Carframe:YK9391J0231L30D32
Wheel:4
Door:4
Apparatus:Kia K3
Color:Color [Wheat]
Copy the code

advantages

1, easy to decouple, decouple the product itself from the product creation process, you can use the same creation process to get different products; 2, easy to accurately control the creation of objects, complex product creation steps into different methods, making the creation process more clear; 3, easy to expand, add a new specific builder without modifying the original class library code, easy to expand, in line with the “open and closed principle”.

disadvantages

1. The products created by the Builder mode generally have more in common and their components are similar; 2. If there is a large difference between products, it is not suitable to use the Builder mode, so its application scope is limited to a certain extent; 3. If the internal changes of the product are complex, many specific builder classes may need to be defined to implement the changes, resulting in a large system.

Usage scenarios

1. The product objects to be generated have complex internal structures, and these product objects have commonality; Isolate the creation and use of complex objects and allow the same creation process to create different products.