Skip to content

Commit

Permalink
Changes the implementation of the prototype pattern (#1103)
Browse files Browse the repository at this point in the history
* Changes the implementation of the prototype pattern

* Fixes the checkstyle warnings

* Fixes additional checkstyle warnings
  • Loading branch information
dgruntz authored and iluwatar committed Nov 19, 2019
1 parent d4b2496 commit 515b7e7
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 89 deletions.
19 changes: 17 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/Beast.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@
/**
* Beast.
*/
public abstract class Beast extends Prototype {
public abstract class Beast implements Prototype {

public Beast() { }

public Beast(Beast source) { }

@Override
public abstract Beast copy();

@Override
public abstract Beast copy() throws CloneNotSupportedException;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}
25 changes: 24 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/ElfBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public ElfBeast(String helpType) {
}

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

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

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ElfBeast other = (ElfBeast) obj;
if (helpType == null) {
if (other.helpType != null) {
return false;
}
} else if (!helpType.equals(other.helpType)) {
return false;
}
return true;
}

}
23 changes: 22 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/ElfMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@
*/
public class ElfMage extends Mage {


private String helpType;

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

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

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ElfMage other = (ElfMage) obj;
if (helpType == null) {
if (other.helpType != null) {
return false;
}
} else if (!helpType.equals(other.helpType)) {
return false;
}
return true;
}
}
24 changes: 23 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/ElfWarlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public ElfWarlord(String helpType) {
}

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

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

}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ElfWarlord other = (ElfWarlord) obj;
if (helpType == null) {
if (other.helpType != null) {
return false;
}
} else if (!helpType.equals(other.helpType)) {
return false;
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,33 +45,21 @@ public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) {
* Create mage.
*/
public Mage createMage() {
try {
return mage.copy();
} catch (CloneNotSupportedException e) {
return null;
}
return mage.copy();
}

/**
* Create warlord.
*/
public Warlord createWarlord() {
try {
return warlord.copy();
} catch (CloneNotSupportedException e) {
return null;
}
return warlord.copy();
}

/**
* Create beast.
*/
public Beast createBeast() {
try {
return beast.copy();
} catch (CloneNotSupportedException e) {
return null;
}
return beast.copy();
}

}
19 changes: 17 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/Mage.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@
/**
* Mage.
*/
public abstract class Mage extends Prototype {
public abstract class Mage implements Prototype {

public Mage() { }

public Mage(Mage source) { }

@Override
public abstract Mage copy();

@Override
public abstract Mage copy() throws CloneNotSupportedException;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}
25 changes: 24 additions & 1 deletion prototype/src/main/java/com/iluwatar/prototype/OrcBeast.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public OrcBeast(String weapon) {
}

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

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

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OrcBeast other = (OrcBeast) obj;
if (weapon == null) {
if (other.weapon != null) {
return false;
}
} else if (!weapon.equals(other.weapon)) {
return false;
}
return true;
}


}
22 changes: 22 additions & 0 deletions prototype/src/main/java/com/iluwatar/prototype/OrcMage.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public OrcMage(String weapon) {
}

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

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OrcMage other = (OrcMage) obj;
if (weapon == null) {
if (other.weapon != null) {
return false;
}
} else if (!weapon.equals(other.weapon)) {
return false;
}
return true;
}
}
22 changes: 22 additions & 0 deletions prototype/src/main/java/com/iluwatar/prototype/OrcWarlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public OrcWarlord(String weapon) {
}

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

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

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (!super.equals(obj)) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OrcWarlord other = (OrcWarlord) obj;
if (weapon == null) {
if (other.weapon != null) {
return false;
}
} else if (!weapon.equals(other.weapon)) {
return false;
}
return true;
}
}
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 abstract class Prototype implements Cloneable {
public interface Prototype {

public abstract Object copy() throws CloneNotSupportedException;
Object copy();

}
19 changes: 17 additions & 2 deletions prototype/src/main/java/com/iluwatar/prototype/Warlord.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,24 @@
/**
* Warlord.
*/
public abstract class Warlord extends Prototype {
public abstract class Warlord implements Prototype {

public Warlord() { }

public Warlord(Warlord source) { }

@Override
public abstract Warlord copy();

@Override
public abstract Warlord copy() throws CloneNotSupportedException;
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
return getClass() == obj.getClass();
}

}

0 comments on commit 515b7e7

Please sign in to comment.