Skip to content

Commit

Permalink
Merge pull request iluwatar#709 from mookkiah/issue_508_prototype
Browse files Browse the repository at this point in the history
issue 508 - using copy constructor to implement prototype.
  • Loading branch information
iluwatar authored Jan 28, 2018
2 parents ec28b12 + 0805716 commit e7b119c
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 47 deletions.
4 changes: 2 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ public static void main(String[] args) {
Warlord warlord;
Beast beast;

factory = new HeroFactoryImpl(new ElfMage(), new ElfWarlord(), new ElfBeast());
factory = new HeroFactoryImpl(new ElfMage("cooking"), new ElfWarlord("cleaning"), new ElfBeast("protecting"));
mage = factory.createMage();
warlord = factory.createWarlord();
beast = factory.createBeast();
LOGGER.info(mage.toString());
LOGGER.info(warlord.toString());
LOGGER.info(beast.toString());

factory = new HeroFactoryImpl(new OrcMage(), new OrcWarlord(), new OrcBeast());
factory = new HeroFactoryImpl(new OrcMage("axe"), new OrcWarlord("sword"), new OrcBeast("laser"));
mage = factory.createMage();
warlord = factory.createWarlord();
beast = factory.createBeast();
Expand Down
2 changes: 1 addition & 1 deletion prototype/src/main/java/com/iluwatar/prototype/Beast.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
public abstract class Beast extends Prototype {

@Override
public abstract Beast clone() throws CloneNotSupportedException;
public abstract Beast copy() throws CloneNotSupportedException;

}
16 changes: 12 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,25 @@
*
*/
public class ElfBeast extends Beast {

private String helpType;

public ElfBeast() {}
public ElfBeast(String helpType) {
this.helpType = helpType;
}

public ElfBeast(ElfBeast elfBeast) {
this.helpType = elfBeast.helpType;
}

@Override
public Beast clone() throws CloneNotSupportedException {
return new ElfBeast();
public Beast copy() throws CloneNotSupportedException {
return new ElfBeast(this);
}

@Override
public String toString() {
return "Elven eagle";
return "Elven eagle helps in " + helpType;
}

}
17 changes: 13 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/ElfMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,25 @@
*/
public class ElfMage extends Mage {

public ElfMage() {}

private String helpType;

public ElfMage(String helpType) {
this.helpType = helpType;
}

public ElfMage(ElfMage elfMage) {
this.helpType = elfMage.helpType;
}

@Override
public Mage clone() throws CloneNotSupportedException {
return new ElfMage();
public ElfMage copy() throws CloneNotSupportedException {
return new ElfMage(this);
}

@Override
public String toString() {
return "Elven mage";
return "Elven mage helps in " + helpType;
}

}
16 changes: 12 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@
*/
public class ElfWarlord extends Warlord {

public ElfWarlord() {}
private String helpType;

public ElfWarlord(String helpType) {
this.helpType = helpType;
}

public ElfWarlord(ElfWarlord elfWarlord) {
this.helpType = elfWarlord.helpType;
}

@Override
public Warlord clone() throws CloneNotSupportedException {
return new ElfWarlord();
public ElfWarlord copy() throws CloneNotSupportedException {
return new ElfWarlord(this);
}

@Override
public String toString() {
return "Elven warlord";
return "Elven warlord helps in " + helpType;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
*/
public Mage createMage() {
try {
return mage.clone();
return mage.copy();
} catch (CloneNotSupportedException e) {
return null;
}
Expand All @@ -58,7 +58,7 @@ public Mage createMage() {
*/
public Warlord createWarlord() {
try {
return warlord.clone();
return warlord.copy();
} catch (CloneNotSupportedException e) {
return null;
}
Expand All @@ -69,7 +69,7 @@ public Warlord createWarlord() {
*/
public Beast createBeast() {
try {
return beast.clone();
return beast.copy();
} catch (CloneNotSupportedException e) {
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion prototype/src/main/java/com/iluwatar/prototype/Mage.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
public abstract class Mage extends Prototype {

@Override
public abstract Mage clone() throws CloneNotSupportedException;
public abstract Mage copy() throws CloneNotSupportedException;

}
17 changes: 13 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,26 @@
*
*/
public class OrcBeast extends Beast {

private String weapon;

public OrcBeast() {}
public OrcBeast(String weapon) {
this.weapon = weapon;
}

public OrcBeast(OrcBeast orcBeast) {
this.weapon = orcBeast.weapon;
}

@Override
public Beast clone() throws CloneNotSupportedException {
return new OrcBeast();
public Beast copy() throws CloneNotSupportedException {
return new OrcBeast(this);
}

@Override
public String toString() {
return "Orcish wolf";
return "Orcish wolf attacks with " + weapon;
}


}
16 changes: 12 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/OrcMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@
*/
public class OrcMage extends Mage {

public OrcMage() {}
private String weapon;

public OrcMage(String weapon) {
this.weapon = weapon;
}

public OrcMage(OrcMage orcMage) {
this.weapon = orcMage.weapon;
}

@Override
public Mage clone() throws CloneNotSupportedException {
return new OrcMage();
public OrcMage copy() throws CloneNotSupportedException {
return new OrcMage(this);
}

@Override
public String toString() {
return "Orcish mage";
return "Orcish mage attacks with " + weapon;
}

}
16 changes: 12 additions & 4 deletions prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,24 @@
*/
public class OrcWarlord extends Warlord {

public OrcWarlord() {}
private String weapon;

public OrcWarlord(String weapon) {
this.weapon = weapon;
}

public OrcWarlord(OrcWarlord orcWarlord) {
this.weapon = orcWarlord.weapon;
}

@Override
public Warlord clone() throws CloneNotSupportedException {
return new OrcWarlord();
public OrcWarlord copy() throws CloneNotSupportedException {
return new OrcWarlord(this);
}

@Override
public String toString() {
return "Orcish warlord";
return "Orcish warlord attacks with " + weapon;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
*/
public abstract class Prototype implements Cloneable {

@Override
public abstract Object clone() throws CloneNotSupportedException;
public abstract Object copy() throws CloneNotSupportedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
public abstract class Warlord extends Prototype {

@Override
public abstract Warlord clone() throws CloneNotSupportedException;
public abstract Warlord copy() throws CloneNotSupportedException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,18 @@ public void testFactory() throws Exception {
final Warlord warlord = mock(Warlord.class);
final Beast beast = mock(Beast.class);

when(mage.clone()).thenThrow(CloneNotSupportedException.class);
when(warlord.clone()).thenThrow(CloneNotSupportedException.class);
when(beast.clone()).thenThrow(CloneNotSupportedException.class);
when(mage.copy()).thenThrow(CloneNotSupportedException.class);
when(warlord.copy()).thenThrow(CloneNotSupportedException.class);
when(beast.copy()).thenThrow(CloneNotSupportedException.class);

final HeroFactoryImpl factory = new HeroFactoryImpl(mage, warlord, beast);
assertNull(factory.createMage());
assertNull(factory.createWarlord());
assertNull(factory.createBeast());

verify(mage).clone();
verify(warlord).clone();
verify(beast).clone();
verify(mage).copy();
verify(warlord).copy();
verify(beast).copy();
verifyNoMoreInteractions(mage, warlord, beast);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
public class PrototypeTest<P extends Prototype> {
static Collection<Object[]> dataProvider() {
return Arrays.asList(
new Object[]{new OrcBeast(), "Orcish wolf"},
new Object[]{new OrcMage(), "Orcish mage"},
new Object[]{new OrcWarlord(), "Orcish warlord"},
new Object[]{new ElfBeast(), "Elven eagle"},
new Object[]{new ElfMage(), "Elven mage"},
new Object[]{new ElfWarlord(), "Elven warlord"}
new Object[]{new OrcBeast("axe"), "Orcish wolf attacks with axe"},
new Object[]{new OrcMage("sword"), "Orcish mage attacks with sword"},
new Object[]{new OrcWarlord("laser"), "Orcish warlord attacks with laser"},
new Object[]{new ElfBeast("cooking"), "Elven eagle helps in cooking"},
new Object[]{new ElfMage("cleaning"), "Elven mage helps in cleaning"},
new Object[]{new ElfWarlord("protecting"), "Elven warlord helps in protecting"}
);
}

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

final Object clone = testedPrototype.clone();
final Object clone = testedPrototype.copy();
assertNotNull(clone);
assertNotSame(clone, testedPrototype);
assertSame(testedPrototype.getClass(), clone.getClass());
Expand Down

0 comments on commit e7b119c

Please sign in to comment.