Home » Design Pattern » Builder

Builder

作者:

分類:

Builder

Builder Concept

The Builder pattern is a creational design pattern, also known as the construction pattern. It involves separating the construction process of a complex object and reassembling it.

The concept is analogous to the division of labor in manufacturing. In the early days, most components of a household appliance were manufactured in-house.

With technological advancements and increasing demands for higher-quality products, specific components are now outsourced to specialized manufacturers. These components are then assembled into the final product.

For example, even devices like the iPhone have their components outsourced and are ultimately assembled by a manufacturing facility. Let’s take a look at the example below.

Builder Example

//**************
//* author: cian
//* 20231022
//**************

class CellPhone
{
    private string cpu = string.empty;
    private string lens = string.empty;
    private string romSize = string.empty;
    
    public void setCpu (string _cpu) { cpu = _cpu; }
    public void setLens (string _lens) { lens = _lens; }
    public void setROMSize (string _romSize) { romSize = _romSize; }
}

abstract class CellPhoneBuilder
{
    protected CellPhone cellPhone;
    
    public void Create() { cellPhone = new CellPhone(); }
    public CellPhone Build() { return cellPhone; }
    
    public abstract void SetCPU();
    public abstract void SetLens();
    public abstract void SetROM();
}

class IPhone15Builder : CellPhoneBuilder
{
    public override void SetCPU() 
    {
        cellPhone.setCpu("A16");
    }
    
    public override void SetLens() 
    {
        cellPhone.setLens("4800M");
    }
    
    public override void SetROM() 
    {
        cellPhone.setROMSize("128GB");
    }
}

class IPhone15ProBuilder : CellPhoneBuilder
{
    public override void SetCPU()
    {
        cellPhone.setCpu("A17");
    }
    
    public override void SetLens()
    {
        cellPhone.setLens("4800M");
    }
    
    public override void SetROM()
    {
        cellPhone.setROMSize("1TB");
    }
}

class CellPhoneFactory
{
    private CellPhoneBuilder builder;

    public void SetBuilder(CellPhoneBuilder _builder) 
    {
        builder = _builder;
    }
    
    public CellPhone GenProduct() 
    {
        builder.Create();
        builder.SetCPU();
        builder.SetLens();
        builder.SetROM();
        return builder.Build();
    }
}

class MainApp
{
    static void Main(string[] args)
    {
        CellPhoneFactory factory = new CellPhoneFactory(); 
        
        factory.SetBuilder(new IPhone15Builder()); 
        var IPhone15 = factory.GenProduct();
        
        factory.SetBuilder(new IPhone15ProBuilder()); 
        var IPhone15PRO = factory.GenProduct();
    }
}

Conclusion

The advantage of the Builder pattern lies in its ability to separate the construction process of the final product.

If there is a need to change the product’s recipe, one can simply replace the production line without affecting the code of the phone or the phone assembly factory.

Additionally, in practical applications, the pattern can be combined with the Factory Method pattern.

In this scenario, the Factory Method is responsible for producing various components, which are then assembled by this pattern.

References

cian Avatar

留言

Leave a Reply

Your email address will not be published. Required fields are marked *