Add creational patterns, Interpreter; remove scripts; update README
This commit is contained in:
12
01-creational/builder/Director.java
Normal file
12
01-creational/builder/Director.java
Normal file
@@ -0,0 +1,12 @@
|
||||
package builder;
|
||||
|
||||
/** Director knows how to build common configurations */
|
||||
public class Director {
|
||||
public House buildStarter(House.Builder builder) {
|
||||
return builder.rooms(2).floors(1).garage(false).roofType("gabled").build();
|
||||
}
|
||||
|
||||
public House buildLuxury(House.Builder builder) {
|
||||
return builder.rooms(6).floors(3).garage(true).garden(true).pool(true).roofType("hip").build();
|
||||
}
|
||||
}
|
||||
43
01-creational/builder/House.java
Normal file
43
01-creational/builder/House.java
Normal file
@@ -0,0 +1,43 @@
|
||||
package builder;
|
||||
|
||||
public class House {
|
||||
private final int rooms;
|
||||
private final int floors;
|
||||
private final boolean hasGarage;
|
||||
private final boolean hasGarden;
|
||||
private final boolean hasPool;
|
||||
private final String roofType;
|
||||
|
||||
private House(Builder builder) {
|
||||
this.rooms = builder.rooms;
|
||||
this.floors = builder.floors;
|
||||
this.hasGarage = builder.hasGarage;
|
||||
this.hasGarden = builder.hasGarden;
|
||||
this.hasPool = builder.hasPool;
|
||||
this.roofType = builder.roofType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "House{rooms=" + rooms + ", floors=" + floors
|
||||
+ ", garage=" + hasGarage + ", garden=" + hasGarden
|
||||
+ ", pool=" + hasPool + ", roof='" + roofType + "'}";
|
||||
}
|
||||
|
||||
public static class Builder {
|
||||
private int rooms = 1;
|
||||
private int floors = 1;
|
||||
private boolean hasGarage = false;
|
||||
private boolean hasGarden = false;
|
||||
private boolean hasPool = false;
|
||||
private String roofType = "flat";
|
||||
|
||||
public Builder rooms(int rooms) { this.rooms = rooms; return this; }
|
||||
public Builder floors(int floors) { this.floors = floors; return this; }
|
||||
public Builder garage(boolean v) { this.hasGarage = v; return this; }
|
||||
public Builder garden(boolean v) { this.hasGarden = v; return this; }
|
||||
public Builder pool(boolean v) { this.hasPool = v; return this; }
|
||||
public Builder roofType(String roofType) { this.roofType = roofType; return this; }
|
||||
public House build() { return new House(this); }
|
||||
}
|
||||
}
|
||||
20
01-creational/builder/Main.java
Normal file
20
01-creational/builder/Main.java
Normal file
@@ -0,0 +1,20 @@
|
||||
package builder;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) {
|
||||
System.out.println("=== Builder Pattern Demo ===\n");
|
||||
|
||||
Director director = new Director();
|
||||
|
||||
House starter = director.buildStarter(new House.Builder());
|
||||
System.out.println("Starter home : " + starter);
|
||||
|
||||
House luxury = director.buildLuxury(new House.Builder());
|
||||
System.out.println("Luxury home : " + luxury);
|
||||
|
||||
// Client builds a custom house directly without the Director
|
||||
House custom = new House.Builder()
|
||||
.rooms(4).floors(2).garden(true).roofType("mansard").build();
|
||||
System.out.println("Custom home : " + custom);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user