Builder Design Patterns
This is one of the easiest and interesting design pattern among other creational design pattern. With use of builder design pattern we can create and object by passing several optional parameters. If the object creation is complex and it requires several parameters for creating an object in that case we can use the builder pattern for creating an object.
Idea of using the builder pattern is we initially create the blank object with no parameters or with the minimum number of parameter and letter on we will add the optional parameter whenever required and will return the created object. In java StringBuffer and StringBuilder objects are created using the builder pattern.
As image describes using HouseBuilder class we can directly create the object of furnished or unfurnished house without exposing the complex object creation login. Let's check the example with code.
House.java
package com.design.pattern.builder; public class House { private String window; private String door; private String bathroom; private String bedroom; private String furniture; public String getWindow() { return window; } public void setWindow(String window) { this.window = window; } public String getDoor() { return door; } public void setDoor(String door) { this.door = door; } public String getBathroom() { return bathroom; } public void setBathroom(String bathroom) { this.bathroom = bathroom; } public String getBedroom() { return bedroom; } public void setBedroom(String bedroom) { this.bedroom = bedroom; } public String getFurniture() { return furniture; } public void setFurniture(String furniture) { this.furniture = furniture; } @Override public String toString() { return "House has "+this.bedroom+", " +this.bathroom+", "+this.getDoor()+", "+this.getWindow()+ (this.getFurniture()!=null?", "+this.getFurniture():""); } }
FurnishedHouseBuilder.java
package com.design.pattern.builder; public class FurnishedHouseBuilder { private House house; public House getHouse() { return house; } public void setHouse(House house) { this.house = house; } public FurnishedHouseBuilder() { house = new House(); } public FurnishedHouseBuilder buildWindow() { house.setWindow("window"); return this; } public FurnishedHouseBuilder buildDoor() { house.setDoor("door"); return this; } public FurnishedHouseBuilder buildBedroom() { house.setBedroom("bedroom"); return this; } public FurnishedHouseBuilder buildBathRoom() { house.setBathroom("bathroom"); return this; } public FurnishedHouseBuilder buildFurniture() { house.setFurniture("furniture"); return this; } }
UnfurnishedHouseBuilder.java
package com.design.pattern.builder; public class UnfurnishedHouseBuilder { private House house; public House getHouse() { return house; } public void setHouse(House house) { this.house = house; } public UnfurnishedHouseBuilder() { house = new House(); } public UnfurnishedHouseBuilder buildWindow() { house.setWindow("window"); return this; } public UnfurnishedHouseBuilder buildDoor() { house.setDoor("door"); return this; } public UnfurnishedHouseBuilder buildBedroom() { house.setBedroom("bedroom"); return this; } public UnfurnishedHouseBuilder buildBathRoom() { house.setBathroom("bathroom"); return this; } }
HouseBuilder.java
package com.design.pattern.builder; public class HouseBuilder { public House buildFurnishedHouse() { return new FurnishedHouseBuilder(). buildBathRoom(). buildBedroom(). buildDoor(). buildWindow(). buildFurniture(). getHouse(); } public House buildUnfurnishedHouse() { return new UnfurnishedHouseBuilder(). buildBathRoom(). buildBedroom(). buildDoor(). buildWindow(). getHouse(); } }
HouseBuilderDemo.java
package com.design.pattern.builder; public class HouseBuilderDemo { public static void main(String[] args) { House furnishedHouse=new HouseBuilder().buildFurnishedHouse(); House unfurnishedHouse=new HouseBuilder().buildUnfurnishedHouse(); System.out.println("Furnished House : "+furnishedHouse ); System.out.println("Unfurnished House : "+unfurnishedHouse ); } }
Output: