Builder Design Pattern
Ganesh Manchi
Full Stack @TCS ? | Ex-SDE @ZopSmart ?? ( Go | Java | React ) ??♂? | 1900+ Knight @Leetcode ?? | Open Source @ Layer5 ?? | CS Engineer ??
Alice and Bob find themselves at a construction site once again.
Alice : Hi Bob, what are you doing in this construction site?
Bob : This is our building, it is in the construction stage.
Alice: Oh! by the way, I newly learned about the Builder Design Pattern.
Bob : What is the purpose of this design pattern?
Alice: Let's take building construction as an example. If you want a 3Bhk with a 1600 Sqft area along with a garden, you can give your requirements to your contractor. The interesting part is that you can provide the requirements in any order, and the contractor is responsible for planning accordingly to fulfill them. Instead of ordering the parameters, we can create another class HouseBuilder. It will be responsible for assigning requirements to the House irrespective of the order.
class House {
private int noOfRooms;
private int areaSqft;
private boolean haveGarden;
private House(HouseBuilder builder) {
this.noOfRooms = builder.noOfRooms;
this.areaSqft = builder.areaSqft;
this.haveGarden = builder.haveGarden;
}
public static class HouseBuilder {
private int noOfRooms;
private int areaSqft;
private boolean haveGarden;
public HouseBuilder rooms(int noOfRooms) {
this.noOfRooms = noOfRooms;
return this;
}
public HouseBuilder area(int areaSqft) {
this.areaSqft = areaSqft;
return this;
}
public HouseBuilder garden(boolean haveGarden) {
this.haveGarden = haveGarden;
return this;
}
public House build() {
return new House(this);
}
}
}
class Main {
/**
* Demonstrates the usage of the Builder pattern to create a House object with flexible parameter order.
*/
public static void main(String[] args) {
// Create a House object with Builder
House house = new House.HouseBuilder()
.area(1600)
.rooms(3)
.garden(true)
.build();
}
}
In this example, inside House, there is a class HouseBuilder. The methods of HouseBuilder set values to its parameters, and the build() method will return a House object by initializing the values through the parameterized constructor House(HouseBuilder builder), which takes the HouseBuilder object as input. In the main() function, we can see that the requirements can be given in any order, and the Builder will assign them to their respective parameters.
领英推荐
Bob : It's a really useful pattern, so that it is not necessary to maintain parameters order while assigning values.
Alice : Not only for simple parameters, but we can also construct a complex object from simple objects along with encapsulating code for construction and representation.
Bob : I'll apply this concept in my project. Thanks for introducing me to a new design pattern.
Alice : You're welcome, Bob. Alright, it's high time; I am going home for Mortal Kombat. You can also join.
Bob : Yeah, I will be there in one hour.
Alice : Ok, let's fight in Mortal Kombat.
Alice and Bob left the construction site, looking forward to their Mortal Kombat match, and they will meet again at Alice's house...
Until then signing off with ? Ganesh