-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package gof.dp.builder; | ||
|
||
public class ChicagoPizza extends Pizza{ | ||
|
||
private final boolean sauceInside; | ||
|
||
public static class Builder extends Pizza.Builder{ | ||
|
||
private boolean sauceInside = false; | ||
|
||
public Builder sauceInside(){ | ||
sauceInside = true; | ||
return this; | ||
} | ||
|
||
@Override | ||
Pizza build() { | ||
return new ChicagoPizza(this); | ||
} | ||
|
||
@Override | ||
protected Pizza.Builder self() { | ||
return this; | ||
} | ||
} | ||
|
||
|
||
ChicagoPizza(Builder builder) { | ||
super(builder); | ||
this.sauceInside = builder.sauceInside; | ||
} | ||
|
||
public String toString() { | ||
return toppings.toString() + " sauceInside: " + sauceInside; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package gof.dp.builder; | ||
|
||
import java.util.Objects; | ||
|
||
public class NewYorkPizza extends Pizza{ | ||
|
||
public enum Size {LARGE, FAMILY, MONSTER}; | ||
private final Size size; | ||
|
||
public static class Builder extends Pizza.Builder{ | ||
private final Size size; | ||
|
||
|
||
public Builder(Size size){ | ||
this.size = Objects.requireNonNull(size); | ||
} | ||
|
||
@Override | ||
public NewYorkPizza build(){ | ||
return new NewYorkPizza(this); | ||
} | ||
|
||
@Override | ||
protected Builder self() { | ||
return this; | ||
} | ||
} | ||
|
||
private NewYorkPizza(Builder builder) { | ||
super(builder); | ||
size = builder.size; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package gof.dp.builder; | ||
|
||
import java.util.EnumSet; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
|
||
public abstract class Pizza { | ||
|
||
public enum Topping {HAM, MUSHROOM, ONION, PEPPER, SAUSAGE}; | ||
final Set<Topping> toppings; | ||
|
||
abstract static class Builder { | ||
EnumSet<Topping> toppings = EnumSet.noneOf(Topping.class); | ||
|
||
public Builder addTopping(Topping topping) { | ||
toppings.add(Objects.requireNonNull(topping)); | ||
return self(); | ||
} | ||
public Builder sauceInside(){ | ||
return self(); | ||
} | ||
|
||
abstract Pizza build(); | ||
protected abstract Builder self(); | ||
} | ||
|
||
Pizza(Builder builder){ | ||
toppings = builder.toppings.clone(); | ||
} | ||
|
||
public String toString(){ | ||
return toppings.toString(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package gof.dp.builder; | ||
|
||
public class PizzaMaster { | ||
|
||
public static void main(String[] args){ | ||
Pizza newYorkPizza = new NewYorkPizza.Builder(NewYorkPizza.Size.LARGE) | ||
// .sauceInside() | ||
.addTopping(Pizza.Topping.HAM) | ||
.addTopping(Pizza.Topping.ONION) | ||
.build(); | ||
|
||
Pizza ChicagoPizza = new ChicagoPizza.Builder() | ||
.sauceInside() | ||
.addTopping(Pizza.Topping.MUSHROOM) | ||
.addTopping(Pizza.Topping.SAUSAGE) | ||
.addTopping(Pizza.Topping.PEPPER) | ||
.build(); | ||
|
||
System.out.println(newYorkPizza); | ||
System.out.println(ChicagoPizza); | ||
} | ||
} |