Skip to content

Commit

Permalink
Prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
dhsim86 committed Jul 29, 2019
1 parent f192644 commit 87974f2
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 0 deletions.
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());
}
}
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;
}

}
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);
}
}

}
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());
}

}
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");
}
}

}

0 comments on commit 87974f2

Please sign in to comment.