The Builder Pattern of Design Patterns

This section talks about the Builder Pattern.

In some cases, a large class needs to rely on other small classes to jointly fulfill a requirement, just as a complete mobile phone needs small parts such as CPU and screen.

The builder pattern is used to deal with this composition process, which is defined as: refers to the separation of the construction of a complex object from its representation, so that the same construction process can create different representations, such a design pattern is called the builder pattern . It decomposes a complex object into multiple simple objects, and then builds it step by step. It separates change from change, that is, the components of the product are unchanged, but each part can be flexibly selected.

The builder pattern consists of two classes: construction class and command class. The construction class is responsible for providing specific construction methods, and the command class is responsible for calling the methods in the construction class to complete the creation of instances. See a complete code below:

//Final product
class Mate
{
    public string CPU { get; set; }
    public string Screen { get; set; }
    public string System { get; set; }
​
    public void LoadCPU(string cpu)
    {
        this.CPU = cpu;
    }
    
    public void LoadScreen(string scr)
    {
        this.Screen = scr;
    }
​
    public void LoadSystem(string sys)
    {
        this.System = sys;
    }
​
    public override string ToString ()
    {
        return $"Mate Stats: \n{CPU}\n{Screen}\n{System}";
    }
}
​
//abstract builder
abstract class ABuilder
{
    protected Mate mate = new Mate ();
​
    public abstract void BuildCPU ();
    public abstract void BuildScreen ();
    public abstract void BuildSystem ();
​
    public Mate Products () => mate;
​
}
​
//specific builder
class BuilderMate : ABuilder
{
    public override void BuildCPU ()
    {
        mate.LoadCPU ("CPU--in place");
    }
​
    public override void BuildScreen ()
    {
        mate.LoadScreen ("Screen--in place");
    }
​
    public override void BuildSystem ()
    {
        mate.LoadSystem ("system--in place");
    }
}
​
//conductor
class Commander
{
    ABuilder bm = null;
    public Commander (ABuilder bm)
    {
        this.bm = bm;
    }
​
    //assemble, produce
    public Mate construct ()
    {
        bm.BuildCPU ();
        bm.BuildScreen ();
        bm.BuildSystem ();
        return bm.Products ();
    }
}

Build the object in the main method:

ABuilder b = new BuilderMate ();
Commander comm = new Commander (b);
Mate m= comm.construct ();
Console.WriteLine (m.ToString ());

 

 

The advantages of the builder pattern are good encapsulation and separation of construction and presentation. Good scalability, each specific builders are independent of each other, which is conducive to the decoupling of the system. The client does not need to know the details of the internal composition of the product, and the builder can gradually refine the creation process without any impact on other modules, which is easy to control the risk of details.

Of course, the shortcomings of the builder mode are also obvious. Each builder class can only serve this production mode. In addition, if the product itself changes, the builder class must also change, and the maintenance cost in the later period is high.

Although the builder pattern and the communist pattern are both used to create objects, the builder pattern focuses on assembly, while the factory pattern focuses on creation. The two can be used in combination, using the factory pattern to create parts and the builder pattern to assemble the final product.

 

Personal public account, love to share, knowledge is priceless.

Tags: Design Pattern

Posted by dmayo2 on Thu, 12 May 2022 03:53:43 +0300