Skip to content

Commit

Permalink
updating Builder code for Design Patterns Bootcamp
Browse files Browse the repository at this point in the history
  • Loading branch information
bethrobson committed Mar 17, 2021
1 parent 985cadd commit a4f71db
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package headfirst.designpatterns.builder.house;

import headfirst.designpatterns.builder.house.HouseBuilder.HouseType;

public class GingerbreadHouseBuilder extends HouseBuilder {
int numWalls = 4;
int numWindows = 4;
String windowMaterial = "Sugar";
String wallMaterial = "Gingerbread";
String wallMaterial = "Gingerbread and icing";
String roofMaterial = "Twizzlers";
public GingerbreadHouseBuilder() {
this.builderName = "Gingerbread House Builder";
Expand All @@ -15,12 +13,14 @@ public GingerbreadHouseBuilder() {
public HouseBuilder addWalls() {
// add exterior walls
for (int i = 0; i < numWalls; i++) {
System.out.println("Adding wall made out of " + wallMaterial);
house.addWall(new Wall(wallMaterial));
}
return this;
}
public HouseBuilder addWindows() {
for (int i = 0; i < numWindows; i++) {
System.out.println("Adding window made out of " + windowMaterial);
house.addWindow(new Window(windowMaterial));
}
return this;
Expand All @@ -29,4 +29,8 @@ public HouseBuilder addRoof() {
house.addRoof(new Roof(roofMaterial));
return this;
}
public House build() {
System.out.println("Stick everything together with icing");
return house;
}
}
7 changes: 4 additions & 3 deletions src/headfirst/designpatterns/builder/house/House.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@ public House() {
public House setHouseType(HouseType houseType) {
this.houseType = houseType;
switch (houseType) {
case STONE: this.name = "My stone house";
case WOOD: this.name = "My wood house";
case GINGERBREAD: this.name = "My holiday gingerbread house";
case WOOD: this.name = "My wood house"; break;
case CLAY: this.name = "My clay house"; break;
case GINGERBREAD: this.name = "My holiday gingerbread house"; break;
case STONE: this.name = "My stone house"; break;
}
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
public abstract class HouseBuilder {
String builderName;
enum HouseType {
STONE, WOOD, GINGERBREAD
WOOD, CLAY, GINGERBREAD, STONE
}
HouseType houseType;
House house = new House();
Expand Down
6 changes: 3 additions & 3 deletions src/headfirst/designpatterns/builder/house/HouseDirector.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ public static void main(String[] args) {
House woodHouse = woodHouseBuilder.addWalls().addWindows().addRoof().build();
System.out.println(woodHouse);

HouseBuilder stoneHouseBuilder = new StoneHouseBuilder();
House stoneHouse = stoneHouseBuilder.addWalls().addWindows().addRoof().build();
System.out.println(stoneHouse);
HouseBuilder clayHouseBuilder = new ClayHouseBuilder();
House clayHouse = clayHouseBuilder.addWalls().addWindows().addRoof().build();
System.out.println(clayHouse);

HouseBuilder gingerbreadHouseBuilder = new GingerbreadHouseBuilder();
House gingerbreadHouse = gingerbreadHouseBuilder.addWalls().addWindows().addRoof().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,8 @@ public HouseBuilder addRoof() {
house.addRoof(new Roof(roofMaterial));
return this;
}
public House build() {
System.out.println("Stick everything together with mortar");
return house;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,19 @@ public WoodHouseBuilder() {
public HouseBuilder addWalls() {
// add exterior walls
for (int i = 0; i < 4; i++) {
System.out.println("Nailing wood for wall made out of " + wallMaterial);
house.addWall(new Wall(wallMaterial));
}
// add interior walls
for (int i = 0; i < numWalls - 4; i++) {
System.out.println("Nailing wood for interior wall made out of " + interiorWallMaterial);
house.addWall(new InteriorWall(interiorWallMaterial));
}
return this;
}
public HouseBuilder addWindows() {
for (int i = 0; i < numWindows; i++) {
System.out.println("Adding window made out of " + windowMaterial);
house.addWindow(new Window(windowMaterial));
}
return this;
Expand All @@ -34,4 +37,8 @@ public HouseBuilder addRoof() {
house.addRoof(new Roof(roofMaterial));
return this;
}
public House build() {
System.out.println("Nail everything together");
return house;
}
}

0 comments on commit a4f71db

Please sign in to comment.