-
Notifications
You must be signed in to change notification settings - Fork 0
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
6 changed files
with
182 additions
and
0 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/dongho/df/domain/creational/builder/pizza/HawaiianPizzaBuilder.java
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,20 @@ | ||
package com.dongho.df.domain.creational.builder.pizza; | ||
|
||
public class HawaiianPizzaBuilder extends PizzaBuilder { | ||
|
||
@Override | ||
public void buildDough() { | ||
pizza.setDough("cross"); | ||
} | ||
|
||
@Override | ||
public void buildSause() { | ||
pizza.setSause("mild"); | ||
} | ||
|
||
@Override | ||
public void buildTopping() { | ||
pizza.setTopping("ham + pineapple"); | ||
} | ||
|
||
} |
32 changes: 32 additions & 0 deletions
32
src/main/java/com/dongho/df/domain/creational/builder/pizza/Pizza.java
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,32 @@ | ||
package com.dongho.df.domain.creational.builder.pizza; | ||
|
||
public class Pizza { | ||
|
||
private String dough = ""; | ||
private String sause = ""; | ||
private String topping = ""; | ||
|
||
public void setDough(String dough) { | ||
this.dough = dough; | ||
} | ||
|
||
public void setSause(String sause) { | ||
this.sause = sause; | ||
} | ||
|
||
public void setTopping(String topping) { | ||
this.topping = topping; | ||
} | ||
|
||
public String getDough() { | ||
return dough; | ||
} | ||
|
||
public String getSause() { | ||
return sause; | ||
} | ||
|
||
public String getTopping() { | ||
return topping; | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/main/java/com/dongho/df/domain/creational/builder/pizza/PizzaBuilder.java
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,27 @@ | ||
package com.dongho.df.domain.creational.builder.pizza; | ||
|
||
public abstract class PizzaBuilder { | ||
|
||
protected Pizza pizza; | ||
|
||
public PizzaBuilder() { | ||
pizza = new Pizza(); | ||
} | ||
|
||
public Pizza getPizza() { | ||
return pizza; | ||
} | ||
|
||
public void buildDough() { | ||
|
||
} | ||
|
||
public void buildSause() { | ||
|
||
} | ||
|
||
public void buildTopping() { | ||
|
||
} | ||
|
||
} |
21 changes: 21 additions & 0 deletions
21
src/main/java/com/dongho/df/domain/creational/builder/pizza/PizzaDirector.java
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,21 @@ | ||
package com.dongho.df.domain.creational.builder.pizza; | ||
|
||
public class PizzaDirector { | ||
|
||
private PizzaBuilder pizzaBuilder; | ||
|
||
public void setPizzaBuilder(PizzaBuilder pizzaBuilder) { | ||
this.pizzaBuilder = pizzaBuilder; | ||
} | ||
|
||
public Pizza getPizza() { | ||
return pizzaBuilder.getPizza(); | ||
} | ||
|
||
public void constructPizza() { | ||
pizzaBuilder.buildDough(); | ||
pizzaBuilder.buildSause(); | ||
pizzaBuilder.buildTopping(); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
src/main/java/com/dongho/df/domain/creational/builder/pizza/SpicyPizzaBuilder.java
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,20 @@ | ||
package com.dongho.df.domain.creational.builder.pizza; | ||
|
||
public class SpicyPizzaBuilder extends PizzaBuilder { | ||
|
||
@Override | ||
public void buildDough() { | ||
pizza.setDough("pan baked"); | ||
} | ||
|
||
@Override | ||
public void buildSause() { | ||
pizza.setSause("hot"); | ||
} | ||
|
||
@Override | ||
public void buildTopping() { | ||
pizza.setTopping("pepperoni + salami"); | ||
} | ||
|
||
} |
62 changes: 62 additions & 0 deletions
62
src/test/java/com/dongho/df/domain/creational/builder/BuilderTest.java
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,62 @@ | ||
package com.dongho.df.domain.creational.builder; | ||
|
||
import com.dongho.df.domain.creational.builder.pizza.HawaiianPizzaBuilder; | ||
import com.dongho.df.domain.creational.builder.pizza.Pizza; | ||
import com.dongho.df.domain.creational.builder.pizza.PizzaDirector; | ||
import com.dongho.df.domain.creational.builder.pizza.SpicyPizzaBuilder; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
@RunWith(JUnit4.class) | ||
public class BuilderTest { | ||
|
||
@Test | ||
public void hawaiianPizzaBuildTest() { | ||
PizzaDirector pizzaDirector; | ||
|
||
given: { | ||
pizzaDirector = new PizzaDirector(); | ||
pizzaDirector.setPizzaBuilder(new HawaiianPizzaBuilder()); | ||
} | ||
|
||
when: { | ||
pizzaDirector.constructPizza(); | ||
} | ||
|
||
then: { | ||
Pizza pizza = pizzaDirector.getPizza(); | ||
|
||
assertThat(pizza.getDough()).isEqualTo("cross"); | ||
assertThat(pizza.getSause()).isEqualTo("mild"); | ||
assertThat(pizza.getTopping()).isEqualTo("ham + pineapple"); | ||
} | ||
|
||
} | ||
|
||
@Test | ||
public void spicyPizzaBuildTest() { | ||
PizzaDirector pizzaDirector; | ||
|
||
given: { | ||
pizzaDirector = new PizzaDirector(); | ||
pizzaDirector.setPizzaBuilder(new SpicyPizzaBuilder()); | ||
} | ||
|
||
when: { | ||
pizzaDirector.constructPizza(); | ||
} | ||
|
||
then: { | ||
Pizza pizza = pizzaDirector.getPizza(); | ||
|
||
assertThat(pizza.getDough()).isEqualTo("pan baked"); | ||
assertThat(pizza.getSause()).isEqualTo("hot"); | ||
assertThat(pizza.getTopping()).isEqualTo("pepperoni + salami"); | ||
} | ||
|
||
} | ||
|
||
} |