Skip to content

RobinKim-SWEngineer/Object-Oriented-Design-2-BuilderPattern

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

Object-Oriented-Design-2-BuilderPattern

Pattern says : "Let me handle the complex construction part separately"

Builder pattern seperates the process of complex construction of an object. It makes the code readable especially when object has lots of properties to be specified.

This pattern would not be neccessary unless the amount of properties is many. The initialization of the object is much more explicit than using class constructor. The order of each setting does not matter.

    VehicleBuilder vehicleBuilder = new VehicleBuilder();
    
    Vehicle hyundaiCar = vehicleBuilder.SetDoors(4)
                                       .SetColor("Red")
                                       .SetManufacturer("Hyundai")
                                       .SetModel("I30")
                                       .SetPrice(450000)
                                       .SetHorsePower(650)
                                       .Build();

In the object builder ( in this example, VehicleBuilder class ) needs to be Reset process that reinitializes the object so the builder can be used again for another object creation. This would be called inside Build(), which is the moment of forwarding the final result of object building steps.

    private void Reset() => _vehicle = new Vehicle();
    
    public Vehicle Build()
        {
            Vehicle vehicle = _vehicle;
            Reset();

            return vehicle;
        }

About

Pattern says : "Let me handle the complex constriction part separately"

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages