Builder Design Pattern is one of the creational design pattern as it is used to solve the problem of creating object having high number of arguments while creating the object.
Other creational Design patterns is -
Factory Design patterns
Singleton Design Pattern
Basically this design patter is used when we have lots of variable needs to initialize while we are instantiating object.
What is Builder Design Pattern ?
Builder Pattern is when piecewise object construction is complicated , providing an API for doing it succinctly.
When to use Builder Design Pattern ?
Explanation
Here also we are doing the same thing just using inheritance .
Hope the code will help you in understanding why we use the builder pattern and how to implement it.
If you have any issue do let us know
Thanks for reading
Noeik
Other creational Design patterns is -
Factory Design patterns
Singleton Design Pattern
Basically this design patter is used when we have lots of variable needs to initialize while we are instantiating object.
What is Builder Design Pattern ?
Builder Pattern is when piecewise object construction is complicated , providing an API for doing it succinctly.
When to use Builder Design Pattern ?
- Having 10 or more constructor arguments in object is very less productive , so we use builder design pattern there.
- When we need to piecewise create the object (Piecewise means we can create the object with few arguments only we dont need to pass all the argument of the object).
There are two type of the Implementation of the Builder Interface
1st way
1st way
Explanation
- We have BuilderDesignOfInterheritanceDemo as a main class where we are initialise the person object
- then we have Person DTO with getter and setters
- PersonBuilder is a Builder Class of Person , We here is showing the we are returning the same instance of the builder class in each method except build() , In build method we will return the main Person Object after all the value will be set
Point to be Noted
In Builder Pattern we always return the Instance of Itself so that we can again all others object
If you have a Scenario when you need to full the value in Object using Inheritance of two Builders
Hope the code will help you in understanding why we use the builder pattern and how to implement it.
Builder Design Pattern Example in JDK
Some of the builder pattern example in Java classes are;
- java.lang.StringBuilder#append() (unsynchronized)
- java.lang.StringBuffer#append() (synchronized)
If you have any issue do let us know
Thanks for reading
Noeik
0 comments:
Post a Comment