Skip to content

Commit e7b119c

Browse files
authoredJan 28, 2018
Merge pull request iluwatar#709 from mookkiah/issue_508_prototype
issue 508 - using copy constructor to implement prototype.
2 parents ec28b12 + 0805716 commit e7b119c

14 files changed

+96
-47
lines changed
 

‎prototype/src/main/java/com/iluwatar/prototype/App.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -52,15 +52,15 @@ public static void main(String[] args) {
5252
Warlord warlord;
5353
Beast beast;
5454

55-
factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
55+
factory = new HeroFactoryImpl(new ElfMage("cooking"), new ElfWarlord("cleaning"), new ElfBeast("protecting"));
5656
mage = factory.createMage();
5757
warlord = factory.createWarlord();
5858
beast = factory.createBeast();
5959
LOGGER.info(mage.toString());
6060
LOGGER.info(warlord.toString());
6161
LOGGER.info(beast.toString());
6262

63-
factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
63+
factory = new HeroFactoryImpl(new OrcMage("axe"), new OrcWarlord("sword"), new OrcBeast("laser"));
6464
mage = factory.createMage();
6565
warlord = factory.createWarlord();
6666
beast = factory.createBeast();

‎prototype/src/main/java/com/iluwatar/prototype/Beast.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
public abstract class Beast extends Prototype {
3131

3232
@Override
33-
public abstract Beast clone() throws CloneNotSupportedException;
33+
public abstract Beast copy() throws CloneNotSupportedException;
3434

3535
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,25 @@
2828
*
2929
*/
3030
public class ElfBeast extends Beast {
31+
32+
private String helpType;
3133

32-
public ElfBeast() {}
34+
public ElfBeast(String helpType) {
35+
this.helpType = helpType;
36+
}
37+
38+
public ElfBeast(ElfBeast elfBeast) {
39+
this.helpType = elfBeast.helpType;
40+
}
3341

3442
@Override
35-
public Beast clone() throws CloneNotSupportedException {
36-
return new ElfBeast();
43+
public Beast copy() throws CloneNotSupportedException {
44+
return new ElfBeast(this);
3745
}
3846

3947
@Override
4048
public String toString() {
41-
return "Elven eagle";
49+
return "Elven eagle helps in " + helpType;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfMage.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,25 @@
2929
*/
3030
public class ElfMage extends Mage {
3131

32-
public ElfMage() {}
32+
33+
private String helpType;
34+
35+
public ElfMage(String helpType) {
36+
this.helpType = helpType;
37+
}
38+
39+
public ElfMage(ElfMage elfMage) {
40+
this.helpType = elfMage.helpType;
41+
}
3342

3443
@Override
35-
public Mage clone() throws CloneNotSupportedException {
36-
return new ElfMage();
44+
public ElfMage copy() throws CloneNotSupportedException {
45+
return new ElfMage(this);
3746
}
3847

3948
@Override
4049
public String toString() {
41-
return "Elven mage";
50+
return "Elven mage helps in " + helpType;
4251
}
4352

4453
}

‎prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
public class ElfWarlord extends Warlord {
3131

32-
public ElfWarlord() {}
32+
private String helpType;
33+
34+
public ElfWarlord(String helpType) {
35+
this.helpType = helpType;
36+
}
37+
38+
public ElfWarlord(ElfWarlord elfWarlord) {
39+
this.helpType = elfWarlord.helpType;
40+
}
3341

3442
@Override
35-
public Warlord clone() throws CloneNotSupportedException {
36-
return new ElfWarlord();
43+
public ElfWarlord copy() throws CloneNotSupportedException {
44+
return new ElfWarlord(this);
3745
}
3846

3947
@Override
4048
public String toString() {
41-
return "Elven warlord";
49+
return "Elven warlord helps in " + helpType;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/HeroFactoryImpl.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
4747
*/
4848
public Mage createMage() {
4949
try {
50-
return mage.clone();
50+
return mage.copy();
5151
} catch (CloneNotSupportedException e) {
5252
return null;
5353
}
@@ -58,7 +58,7 @@ public Mage createMage() {
5858
*/
5959
public Warlord createWarlord() {
6060
try {
61-
return warlord.clone();
61+
return warlord.copy();
6262
} catch (CloneNotSupportedException e) {
6363
return null;
6464
}
@@ -69,7 +69,7 @@ public Warlord createWarlord() {
6969
*/
7070
public Beast createBeast() {
7171
try {
72-
return beast.clone();
72+
return beast.copy();
7373
} catch (CloneNotSupportedException e) {
7474
return null;
7575
}

‎prototype/src/main/java/com/iluwatar/prototype/Mage.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
public abstract class Mage extends Prototype {
3131

3232
@Override
33-
public abstract Mage clone() throws CloneNotSupportedException;
33+
public abstract Mage copy() throws CloneNotSupportedException;
3434

3535
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java

+13-4
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,26 @@
2828
*
2929
*/
3030
public class OrcBeast extends Beast {
31+
32+
private String weapon;
3133

32-
public OrcBeast() {}
34+
public OrcBeast(String weapon) {
35+
this.weapon = weapon;
36+
}
37+
38+
public OrcBeast(OrcBeast orcBeast) {
39+
this.weapon = orcBeast.weapon;
40+
}
3341

3442
@Override
35-
public Beast clone() throws CloneNotSupportedException {
36-
return new OrcBeast();
43+
public Beast copy() throws CloneNotSupportedException {
44+
return new OrcBeast(this);
3745
}
3846

3947
@Override
4048
public String toString() {
41-
return "Orcish wolf";
49+
return "Orcish wolf attacks with " + weapon;
4250
}
51+
4352

4453
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcMage.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
public class OrcMage extends Mage {
3131

32-
public OrcMage() {}
32+
private String weapon;
33+
34+
public OrcMage(String weapon) {
35+
this.weapon = weapon;
36+
}
37+
38+
public OrcMage(OrcMage orcMage) {
39+
this.weapon = orcMage.weapon;
40+
}
3341

3442
@Override
35-
public Mage clone() throws CloneNotSupportedException {
36-
return new OrcMage();
43+
public OrcMage copy() throws CloneNotSupportedException {
44+
return new OrcMage(this);
3745
}
3846

3947
@Override
4048
public String toString() {
41-
return "Orcish mage";
49+
return "Orcish mage attacks with " + weapon;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java

+12-4
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,24 @@
2929
*/
3030
public class OrcWarlord extends Warlord {
3131

32-
public OrcWarlord() {}
32+
private String weapon;
33+
34+
public OrcWarlord(String weapon) {
35+
this.weapon = weapon;
36+
}
37+
38+
public OrcWarlord(OrcWarlord orcWarlord) {
39+
this.weapon = orcWarlord.weapon;
40+
}
3341

3442
@Override
35-
public Warlord clone() throws CloneNotSupportedException {
36-
return new OrcWarlord();
43+
public OrcWarlord copy() throws CloneNotSupportedException {
44+
return new OrcWarlord(this);
3745
}
3846

3947
@Override
4048
public String toString() {
41-
return "Orcish warlord";
49+
return "Orcish warlord attacks with " + weapon;
4250
}
4351

4452
}

‎prototype/src/main/java/com/iluwatar/prototype/Prototype.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
*/
3030
public abstract class Prototype implements Cloneable {
3131

32-
@Override
33-
public abstract Object clone() throws CloneNotSupportedException;
32+
public abstract Object copy() throws CloneNotSupportedException;
3433

3534
}

‎prototype/src/main/java/com/iluwatar/prototype/Warlord.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,6 @@
3030
public abstract class Warlord extends Prototype {
3131

3232
@Override
33-
public abstract Warlord clone() throws CloneNotSupportedException;
33+
public abstract Warlord copy() throws CloneNotSupportedException;
3434

3535
}

‎prototype/src/test/java/com/iluwatar/prototype/HeroFactoryImplTest.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ public void testFactory() throws Exception {
4343
final Warlord warlord = mock(Warlord.class);
4444
final Beast beast = mock(Beast.class);
4545

46-
when(mage.clone()).thenThrow(CloneNotSupportedException.class);
47-
when(warlord.clone()).thenThrow(CloneNotSupportedException.class);
48-
when(beast.clone()).thenThrow(CloneNotSupportedException.class);
46+
when(mage.copy()).thenThrow(CloneNotSupportedException.class);
47+
when(warlord.copy()).thenThrow(CloneNotSupportedException.class);
48+
when(beast.copy()).thenThrow(CloneNotSupportedException.class);
4949

5050
final HeroFactoryImpl factory = new HeroFactoryImpl(mage, warlord, beast);
5151
assertNull(factory.createMage());
5252
assertNull(factory.createWarlord());
5353
assertNull(factory.createBeast());
5454

55-
verify(mage).clone();
56-
verify(warlord).clone();
57-
verify(beast).clone();
55+
verify(mage).copy();
56+
verify(warlord).copy();
57+
verify(beast).copy();
5858
verifyNoMoreInteractions(mage, warlord, beast);
5959
}
6060

‎prototype/src/test/java/com/iluwatar/prototype/PrototypeTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@
4141
public class PrototypeTest<P extends Prototype> {
4242
static Collection<Object[]> dataProvider() {
4343
return Arrays.asList(
44-
new Object[]{new OrcBeast(), "Orcish wolf"},
45-
new Object[]{new OrcMage(), "Orcish mage"},
46-
new Object[]{new OrcWarlord(), "Orcish warlord"},
47-
new Object[]{new ElfBeast(), "Elven eagle"},
48-
new Object[]{new ElfMage(), "Elven mage"},
49-
new Object[]{new ElfWarlord(), "Elven warlord"}
44+
new Object[]{new OrcBeast("axe"), "Orcish wolf attacks with axe"},
45+
new Object[]{new OrcMage("sword"), "Orcish mage attacks with sword"},
46+
new Object[]{new OrcWarlord("laser"), "Orcish warlord attacks with laser"},
47+
new Object[]{new ElfBeast("cooking"), "Elven eagle helps in cooking"},
48+
new Object[]{new ElfMage("cleaning"), "Elven mage helps in cleaning"},
49+
new Object[]{new ElfWarlord("protecting"), "Elven warlord helps in protecting"}
5050
);
5151
}
5252

@@ -55,7 +55,7 @@ static Collection<Object[]> dataProvider() {
5555
public void testPrototype(P testedPrototype, String expectedToString) throws Exception {
5656
assertEquals(expectedToString, testedPrototype.toString());
5757

58-
final Object clone = testedPrototype.clone();
58+
final Object clone = testedPrototype.copy();
5959
assertNotNull(clone);
6060
assertNotSame(clone, testedPrototype);
6161
assertSame(testedPrototype.getClass(), clone.getClass());

0 commit comments

Comments
 (0)