Task - Design Patterns - Builder Pattern with Factory Method #71
akash-coded
started this conversation in
Tasks
Replies: 3 comments
-
updated Civil Engineer class codeclass CivilEngineer {
private HouseBuilder houseBuilder;
CivilEngineer(String type) {
this.houseBuilder = getHouseBuilder(type);
}
public void constructHouse() {
this.houseBuilder.buildBasement();
this.houseBuilder.buildStructure();
this.houseBuilder.buildRoof();
this.houseBuilder.buildInterior();
}
public House getHouse() {
return this.houseBuilder.getHouse();
}
public HouseBuilder getHouseBuilder(String type) {
switch (type) {
case "Wood":
return new WoodenHouseBuilder();
case "Ice":
return new IglooHouseBuilder();
default:
throw new IllegalArgumentException("Invalid House Type");
}
}
}
public class BuilderDemo {
public static void main(String[] args) {
CivilEngineer engineer = new CivilEngineer("Ice");
engineer.constructHouse();
House house = engineer.getHouse();
System.out.println("Finished house::\n" + house);
}
}Screenshot |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Kishore Kunal class CivilEngineer { } public class BuilderDemo { } |
Beta Was this translation helpful? Give feedback.
0 replies
-
Civil Engineer Classclass CivilEngineer {
private HouseBuilder houseBuilder;
private String houseModel;
CivilEngineer(String houseModel) {
this.houseModel = houseModel;
}
public void constructHouse() {
this.houseBuilder.buildBasement();
this.houseBuilder.buildStructure();
this.houseBuilder.buildRoof();
this.houseBuilder.buildInterior();
}
public House getHouse() {
return this.houseBuilder.getHouse();
}
public void getHouseType() {
if(this.houseModel.equals("IgloHouse")){
this.houseBuilder = new IglooHouseBuilder();
}else {
this.houseBuilder = new WoodenHouseBuilder();
}
}
}Driver classpublic class BuilderDemo {
public static void main(String[] args) {
CivilEngineer engineer = new CivilEngineer("WoodenHouse");
engineer.getHouseType();
engineer.constructHouse();
House house = engineer.getHouse();
System.out.println("Finished house::\n" + house);
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment


Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
In the demonstrative code for the Builder pattern, the client (
main()method) has to create an instance of aHouseBuilderand pass it for the instantiation of theCivilEngineer.Your objective is to use the Factory Method pattern to eliminate the need to create an instance of a
HouseBuilderby the client. The client should only interact with theCivilEngineerand provide the desired house to theCivilEngineerin the form of aStringvalue.You can refer to the Factory Method pattern demo code here. And, you can refer to this whiteboard for the illustrations and theoretical concepts of different design patterns.
The code without the Factory Method is given below. Incorporate the Factory Method pattern in it and post the solution in the comments.
Beta Was this translation helpful? Give feedback.
All reactions