c# design pattern-builder pattern of creational pattern

Introduction:

        Separate the construction and representation of a complex object so that the same construction process can create different representations. Provides an optimal way to create objects. A Builder class constructs the final object step by step. The Builder class is independent of other objects. The intention is to separate a complex build from its representation so that the same build process can create different representations.
        The core idea of ​​the builder pattern is to separate the construction of a complex object from its representation, so that users only need to focus on the properties of the object that needs to be constructed. In this way, users do not need to know the internal implementation details of the object. They only need to set the object properties through the interface methods provided by Builder to get a complete object.
Builder mode includes the following roles:
  1. Abstract builder class ( Builder ): This interface specifies the creation of those parts of a complex object and does not involve the creation of specific component objects.
  2. Concrete builder class ( ConcreteBuilder ): implements the Builder interface and completes the specific creation methods of each component of a complex product. After the construction process is complete, provide an instance of the product.
  3. Product class ( Product ): The complex object to be created.
  4. Director class ( Director ): Calls specific builders to create various parts of a complex object. The director does not involve specific product information and is only responsible for ensuring that all parts of the object are created completely or in a certain order.

code demo

Let's demonstrate using code, using the builder method to create a bicycle

First, create a Bike class to provide three attributes for the bicycle: basket, rack, and seat bag.

public class Bike
{
    public string basket { get; set; }
    public string frame { get; set; }
    public string seat { get; set; }
}

Abstract builder class ( Builder )

The bike is instantiated here, and there are three abstract methods to build the basket, rack, and seat bag respectively. Because the builder calls the specific builder to create it, we override the method in the implementation class for the specific business.

public abstract class Builder
{
    protected Bike mBike = new Bike();

    public abstract void buildBasket();

    public abstract void buildFrame();

    public abstract void buildSeat();

    public Bike createBike()
    {
        return mBike;
    }
}

Concrete builder class (ConcreteBuilder)

Two implementation classes are written here to create different bicycles

//山地自行车
public class MountainBuilder : Builder
{
    public override void buildBasket()
    {
        mBike.basket = "蓝色车篮";
    }

    public override void buildFrame()
    {
        mBike.frame = "铝合金车架";
    }

    public override void buildSeat()
    {
        mBike.seat = "橡胶车座";
    }
}

//公路自行车
public class RoadBuilder : Builder
{
    public override void buildBasket()
    {
        mBike.basket = "黑色车篮";
    }

    public override void buildFrame()
    {
        mBike.frame = "碳纤维车架";
    }

    public override void buildSeat()
    {
        mBike.seat = "海绵车座";
    }
}

Director class ( Director )

Responsible for ensuring that all parts of the object are created completely or in a certain order. Here, their build methods are called in sequence based on the builder implementation class passed in.

public class Director
{
    private Builder _builder;

    public Director(Builder builder)
    {
        _builder = builder;
    }

    public Bike construct()
    {
        _builder.buildBasket();
        _builder.buildFrame();
        _builder.buildSeat();
        return _builder.createBike();
    }
}

have a test

Create a Director instance with a road car instance in the parameter and call its construct method to get a road car.

    public static void Main(string[] args)
    {
        var director = new Director(new RoadBuilder());
        Bike bike = director.construct();
        Console.WriteLine($"车篮:{bike.basket};  车架:{bike.frame};  车座:{bike.seat}");
    }

If you want to change to a mountain bike, you only need to change the parameters to an instance of a mountain bike.

var director = new Director(new MountainBuilder());

 

 If there is a requirement now that all bicycles created using this builder mode need to remove the bicycle, we only need to remove the corresponding basket creation method in the Director class.

At this time, all bicycle instances created by calling the commander's construct have no baskets.

Summarize:

advantage:
  • The builder pattern is very encapsulated. Using the builder pattern can effectively encapsulate changes. In the scenario of using the builder pattern, the general product class and builder class are relatively stable. Therefore, encapsulating the main business logic in the commander class can achieve overall results. Better stability.
  • In the builder pattern, the client does not have to know the details of the internal composition of the product, decoupling the product itself from the product creation process, so that the same creation process can create different product objects. You can have more granular control over the product creation process. Breaking down the creation steps of complex products into different methods makes the creation process clearer and makes it easier to use programs to control the creation process.
  •  The builder pattern is easily extensible. If there are new requirements, it can be completed by implementing a new builder class. Basically, there is no need to modify the code that has been tested before, so there will be no risk to the original functions. Comply with the opening and closing principle.
shortcoming:
  • The products created by the builder mode generally have many things in common and their components are similar. If the differences between the products are large, the builder mode is not suitable, so its scope of use is subject to certain restrictions.
scenes to be used:
  • The created object is more complex and consists of multiple parts. Each part faces complex changes, but the construction sequence between components is stable.
  • The algorithms used to create complex objects are independent of the object's component parts and how they are assembled, i.e. the building process and final representation are independent.

Guess you like

Origin blog.csdn.net/weixin_65243968/article/details/132867144