-
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
5 changed files
with
124 additions
and
0 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
src/main/java/com/dongho/df/domain/creational/prototype/ChocolateCookie.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,14 @@ | ||
package com.dongho.df.domain.creational.prototype; | ||
|
||
public class ChocolateCookie extends CookiePrototype { | ||
|
||
@Override | ||
public String getName() { | ||
return "chocolateCookie"; | ||
} | ||
|
||
@Override | ||
protected ChocolateCookie clone() { | ||
return ChocolateCookie.class.cast(super.clone()); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/java/com/dongho/df/domain/creational/prototype/CookieMachine.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,19 @@ | ||
package com.dongho.df.domain.creational.prototype; | ||
|
||
public class CookieMachine { | ||
|
||
private CookiePrototype cookiePrototype; | ||
|
||
public void setCookiePrototype(CookiePrototype cookiePrototype) { | ||
this.cookiePrototype = cookiePrototype; | ||
} | ||
|
||
public CookiePrototype makeCookie() { | ||
CookiePrototype cookie = cookiePrototype.clone(); | ||
|
||
// Do something for another initialization. | ||
|
||
return cookie; | ||
} | ||
|
||
} |
18 changes: 18 additions & 0 deletions
18
src/main/java/com/dongho/df/domain/creational/prototype/CookiePrototype.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,18 @@ | ||
package com.dongho.df.domain.creational.prototype; | ||
|
||
public class CookiePrototype implements Cloneable { | ||
|
||
public String getName() { | ||
return "prototypeName"; | ||
} | ||
|
||
@Override | ||
protected CookiePrototype clone() { | ||
try { | ||
return CookiePrototype.class.cast(super.clone()); | ||
} catch (CloneNotSupportedException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
15 changes: 15 additions & 0 deletions
15
src/main/java/com/dongho/df/domain/creational/prototype/StrawberryCookie.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,15 @@ | ||
package com.dongho.df.domain.creational.prototype; | ||
|
||
public class StrawberryCookie extends CookiePrototype { | ||
|
||
@Override | ||
public String getName() { | ||
return "strawberryCookie"; | ||
} | ||
|
||
@Override | ||
protected StrawberryCookie clone() { | ||
return StrawberryCookie.class.cast(super.clone()); | ||
} | ||
|
||
} |
58 changes: 58 additions & 0 deletions
58
src/test/java/com/dongho/df/domain/creational/prototype/PrototypeTest.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,58 @@ | ||
package com.dongho.df.domain.creational.prototype; | ||
|
||
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 PrototypeTest { | ||
|
||
@Test | ||
public void chocolateCookieTest() { | ||
CookieMachine cookieMachine; | ||
CookiePrototype cookiePrototype1; | ||
CookiePrototype cookiePrototype2; | ||
|
||
given: { | ||
cookieMachine = new CookieMachine(); | ||
cookieMachine.setCookiePrototype(new ChocolateCookie()); | ||
} | ||
|
||
when: { | ||
cookiePrototype1 = cookieMachine.makeCookie(); | ||
cookiePrototype2 = cookieMachine.makeCookie(); | ||
} | ||
|
||
then: { | ||
assertThat(cookiePrototype1).isNotEqualTo(cookiePrototype2); | ||
assertThat(cookiePrototype1.getName()).isEqualTo("chocolateCookie"); | ||
assertThat(cookiePrototype2.getName()).isEqualTo("chocolateCookie"); | ||
} | ||
} | ||
|
||
@Test | ||
public void strawberryCookieTest() { | ||
CookieMachine cookieMachine; | ||
CookiePrototype cookiePrototype1; | ||
CookiePrototype cookiePrototype2; | ||
|
||
given: { | ||
cookieMachine = new CookieMachine(); | ||
cookieMachine.setCookiePrototype(new StrawberryCookie()); | ||
} | ||
|
||
when: { | ||
cookiePrototype1 = cookieMachine.makeCookie(); | ||
cookiePrototype2 = cookieMachine.makeCookie(); | ||
} | ||
|
||
then: { | ||
assertThat(cookiePrototype1).isNotEqualTo(cookiePrototype2); | ||
assertThat(cookiePrototype1.getName()).isEqualTo("strawberryCookie"); | ||
assertThat(cookiePrototype2.getName()).isEqualTo("strawberryCookie"); | ||
} | ||
} | ||
|
||
} |