Skip to content

Refactor Prototype pattern #1838

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/Beast.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public Beast(Beast source) {
}

@Override
public abstract Beast copy();
public Beast clone() {
try {
return (Beast) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

}
4 changes: 2 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public ElfBeast(ElfBeast elfBeast) {
}

@Override
public ElfBeast copy() {
return new ElfBeast(this);
public ElfBeast clone() {
return (ElfBeast) super.clone();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/ElfMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public ElfMage(ElfMage elfMage) {
}

@Override
public ElfMage copy() {
return new ElfMage(this);
public ElfMage clone() {
return (ElfMage) super.clone();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/**
* ElfWarlord.
*/
@EqualsAndHashCode
@EqualsAndHashCode(callSuper = true)
public class ElfWarlord extends Warlord {

private final String helpType;
Expand All @@ -43,12 +43,12 @@ public ElfWarlord(ElfWarlord elfWarlord) {
}

@Override
public ElfWarlord copy() {
return new ElfWarlord(this);
public ElfWarlord clone() {
return (ElfWarlord) super.clone();
}

@Override
public String toString() {
return "Elven warlord helps in " + helpType;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,21 @@ public class HeroFactoryImpl implements HeroFactory {
* Create mage.
*/
public Mage createMage() {
return mage.copy();
return mage.clone();
}

/**
* Create warlord.
*/
public Warlord createWarlord() {
return warlord.copy();
return warlord.clone();
}

/**
* Create beast.
*/
public Beast createBeast() {
return beast.copy();
return beast.clone();
}

}
8 changes: 7 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/Mage.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public Mage(Mage source) {
}

@Override
public abstract Mage copy();
public Mage clone() {
try {
return (Mage) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

}
6 changes: 3 additions & 3 deletions prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
/**
* OrcBeast.
*/
@EqualsAndHashCode(callSuper = false)
@EqualsAndHashCode(callSuper = true)
@RequiredArgsConstructor
public class OrcBeast extends Beast {

Expand All @@ -41,8 +41,8 @@ public OrcBeast(OrcBeast orcBeast) {
}

@Override
public OrcBeast copy() {
return new OrcBeast(this);
public OrcBeast clone() {
return (OrcBeast) super.clone();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/OrcMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public OrcMage(OrcMage orcMage) {
}

@Override
public OrcMage copy() {
return new OrcMage(this);
public OrcMage clone() {
return (OrcMage) super.clone();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ public OrcWarlord(OrcWarlord orcWarlord) {
}

@Override
public OrcWarlord copy() {
return new OrcWarlord(this);
public OrcWarlord clone() {
return (OrcWarlord) super.clone();
}

@Override
Expand Down
4 changes: 2 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/Prototype.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
/**
* Prototype.
*/
public interface Prototype {
public interface Prototype extends Cloneable {

Object copy();
Prototype clone();

}
8 changes: 7 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/Warlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ public Warlord(Warlord source) {
}

@Override
public abstract Warlord copy();
public Warlord clone() {
try {
return (Warlord) super.clone();
} catch (CloneNotSupportedException e) {
throw new RuntimeException(e);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ static Collection<Object[]> dataProvider() {
void testPrototype(P testedPrototype, String expectedToString) {
assertEquals(expectedToString, testedPrototype.toString());

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