Skip to content

Commit 515b7e7

Browse files
dgruntziluwatar
authored andcommitted
Changes the implementation of the prototype pattern (iluwatar#1103)
* Changes the implementation of the prototype pattern * Fixes the checkstyle warnings * Fixes additional checkstyle warnings
1 parent d4b2496 commit 515b7e7

File tree

13 files changed

+194
-89
lines changed

13 files changed

+194
-89
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,24 @@
2626
/**
2727
* Beast.
2828
*/
29-
public abstract class Beast extends Prototype {
29+
public abstract class Beast implements Prototype {
30+
31+
public Beast() { }
32+
33+
public Beast(Beast source) { }
34+
35+
@Override
36+
public abstract Beast copy();
3037

3138
@Override
32-
public abstract Beast copy() throws CloneNotSupportedException;
39+
public boolean equals(Object obj) {
40+
if (this == obj) {
41+
return true;
42+
}
43+
if (obj == null) {
44+
return false;
45+
}
46+
return getClass() == obj.getClass();
47+
}
3348

3449
}

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public ElfBeast(String helpType) {
3535
}
3636

3737
public ElfBeast(ElfBeast elfBeast) {
38+
super(elfBeast);
3839
this.helpType = elfBeast.helpType;
3940
}
4041

4142
@Override
42-
public Beast copy() {
43+
public ElfBeast copy() {
4344
return new ElfBeast(this);
4445
}
4546

@@ -48,4 +49,26 @@ public String toString() {
4849
return "Elven eagle helps in " + helpType;
4950
}
5051

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
ElfBeast other = (ElfBeast) obj;
64+
if (helpType == null) {
65+
if (other.helpType != null) {
66+
return false;
67+
}
68+
} else if (!helpType.equals(other.helpType)) {
69+
return false;
70+
}
71+
return true;
72+
}
73+
5174
}

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
*/
2929
public class ElfMage extends Mage {
3030

31-
3231
private String helpType;
3332

3433
public ElfMage(String helpType) {
3534
this.helpType = helpType;
3635
}
3736

3837
public ElfMage(ElfMage elfMage) {
38+
super(elfMage);
3939
this.helpType = elfMage.helpType;
4040
}
4141

@@ -49,4 +49,25 @@ public String toString() {
4949
return "Elven mage helps in " + helpType;
5050
}
5151

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
ElfMage other = (ElfMage) obj;
64+
if (helpType == null) {
65+
if (other.helpType != null) {
66+
return false;
67+
}
68+
} else if (!helpType.equals(other.helpType)) {
69+
return false;
70+
}
71+
return true;
72+
}
5273
}

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

+23-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public ElfWarlord(String helpType) {
3535
}
3636

3737
public ElfWarlord(ElfWarlord elfWarlord) {
38+
super(elfWarlord);
3839
this.helpType = elfWarlord.helpType;
3940
}
4041

@@ -48,4 +49,25 @@ public String toString() {
4849
return "Elven warlord helps in " + helpType;
4950
}
5051

51-
}
52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
ElfWarlord other = (ElfWarlord) obj;
64+
if (helpType == null) {
65+
if (other.helpType != null) {
66+
return false;
67+
}
68+
} else if (!helpType.equals(other.helpType)) {
69+
return false;
70+
}
71+
return true;
72+
}
73+
}

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

+3-15
Original file line numberDiff line numberDiff line change
@@ -45,33 +45,21 @@ public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
4545
* Create mage.
4646
*/
4747
public Mage createMage() {
48-
try {
49-
return mage.copy();
50-
} catch (CloneNotSupportedException e) {
51-
return null;
52-
}
48+
return mage.copy();
5349
}
5450

5551
/**
5652
* Create warlord.
5753
*/
5854
public Warlord createWarlord() {
59-
try {
60-
return warlord.copy();
61-
} catch (CloneNotSupportedException e) {
62-
return null;
63-
}
55+
return warlord.copy();
6456
}
6557

6658
/**
6759
* Create beast.
6860
*/
6961
public Beast createBeast() {
70-
try {
71-
return beast.copy();
72-
} catch (CloneNotSupportedException e) {
73-
return null;
74-
}
62+
return beast.copy();
7563
}
7664

7765
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,24 @@
2626
/**
2727
* Mage.
2828
*/
29-
public abstract class Mage extends Prototype {
29+
public abstract class Mage implements Prototype {
30+
31+
public Mage() { }
32+
33+
public Mage(Mage source) { }
34+
35+
@Override
36+
public abstract Mage copy();
3037

3138
@Override
32-
public abstract Mage copy() throws CloneNotSupportedException;
39+
public boolean equals(Object obj) {
40+
if (this == obj) {
41+
return true;
42+
}
43+
if (obj == null) {
44+
return false;
45+
}
46+
return getClass() == obj.getClass();
47+
}
3348

3449
}

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

+24-1
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public OrcBeast(String weapon) {
3535
}
3636

3737
public OrcBeast(OrcBeast orcBeast) {
38+
super(orcBeast);
3839
this.weapon = orcBeast.weapon;
3940
}
4041

4142
@Override
42-
public Beast copy() {
43+
public OrcBeast copy() {
4344
return new OrcBeast(this);
4445
}
4546

@@ -48,5 +49,27 @@ public String toString() {
4849
return "Orcish wolf attacks with " + weapon;
4950
}
5051

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
OrcBeast other = (OrcBeast) obj;
64+
if (weapon == null) {
65+
if (other.weapon != null) {
66+
return false;
67+
}
68+
} else if (!weapon.equals(other.weapon)) {
69+
return false;
70+
}
71+
return true;
72+
}
73+
5174

5275
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public OrcMage(String weapon) {
3535
}
3636

3737
public OrcMage(OrcMage orcMage) {
38+
super(orcMage);
3839
this.weapon = orcMage.weapon;
3940
}
4041

@@ -48,4 +49,25 @@ public String toString() {
4849
return "Orcish mage attacks with " + weapon;
4950
}
5051

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
OrcMage other = (OrcMage) obj;
64+
if (weapon == null) {
65+
if (other.weapon != null) {
66+
return false;
67+
}
68+
} else if (!weapon.equals(other.weapon)) {
69+
return false;
70+
}
71+
return true;
72+
}
5173
}

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

+22
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public OrcWarlord(String weapon) {
3535
}
3636

3737
public OrcWarlord(OrcWarlord orcWarlord) {
38+
super(orcWarlord);
3839
this.weapon = orcWarlord.weapon;
3940
}
4041

@@ -48,4 +49,25 @@ public String toString() {
4849
return "Orcish warlord attacks with " + weapon;
4950
}
5051

52+
@Override
53+
public boolean equals(Object obj) {
54+
if (this == obj) {
55+
return true;
56+
}
57+
if (!super.equals(obj)) {
58+
return false;
59+
}
60+
if (getClass() != obj.getClass()) {
61+
return false;
62+
}
63+
OrcWarlord other = (OrcWarlord) obj;
64+
if (weapon == null) {
65+
if (other.weapon != null) {
66+
return false;
67+
}
68+
} else if (!weapon.equals(other.weapon)) {
69+
return false;
70+
}
71+
return true;
72+
}
5173
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
/**
2727
* Prototype.
2828
*/
29-
public abstract class Prototype implements Cloneable {
29+
public interface Prototype {
3030

31-
public abstract Object copy() throws CloneNotSupportedException;
31+
Object copy();
3232

3333
}

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,24 @@
2626
/**
2727
* Warlord.
2828
*/
29-
public abstract class Warlord extends Prototype {
29+
public abstract class Warlord implements Prototype {
30+
31+
public Warlord() { }
32+
33+
public Warlord(Warlord source) { }
34+
35+
@Override
36+
public abstract Warlord copy();
3037

3138
@Override
32-
public abstract Warlord copy() throws CloneNotSupportedException;
39+
public boolean equals(Object obj) {
40+
if (this == obj) {
41+
return true;
42+
}
43+
if (obj == null) {
44+
return false;
45+
}
46+
return getClass() == obj.getClass();
47+
}
3348

3449
}

0 commit comments

Comments
 (0)