When we have an application with different working parts and all have to be closed in one object builder pattern can help make things pretty easy when making objects out of our class. Generally in builder pattern rather than setting the object properties inside the constructor we set them using set() methods. This makes our class objects flexible and let us define or not define the properties we want and do not want.
Lets dive straight into it with an example of User class and Address class, our User object will have address as Address object.
In this code above when we are not defining values for age and email we have to pass undefined
, if undefined is not passed the value of phone get assigned to age. This type of approach is clearly a problem as it will get tough to define properties for an object with many properties. It will be a nightmare tbh!
So we can fix this by creating another class that will help us build a User but with no pain.
Builder Pattern with a builder
Lets start by defining a Builder class and define some methods for age,phone,email and address.
Passing Objects to create instance
Another way of building object can be achieved by simply passing in an object when we create an instance of User. If we pass an object to define the properties of User it then automatically can take care of what to set and what not.
So we just refactor our User class into something like
In the above code we are assigning {age,phone,email,address}
to = {}
in the constructor parameters which means if nothing is passed then pass in an empty object. We can also set default values for object if nothing is set by assigning it like this {age=10,phone,email,address}
.
Both ways of implementing builder patterns helps us fix the issue of instantiating big objects. However using a builder class method can still prove to be a better approach when the object we constructing is complex.
happy building!