Changes the implementation of the prototype pattern (#1103)
* Changes the implementation of the prototype pattern * Fixes the checkstyle warnings * Fixes additional checkstyle warnings
This commit is contained in:
committed by
Ilkka Seppälä
parent
d4b2496e60
commit
515b7e7134
@ -26,9 +26,24 @@ package com.iluwatar.prototype;
|
||||
/**
|
||||
* Beast.
|
||||
*/
|
||||
public abstract class Beast extends Prototype {
|
||||
public abstract class Beast implements Prototype {
|
||||
|
||||
public Beast() { }
|
||||
|
||||
public Beast(Beast source) { }
|
||||
|
||||
@Override
|
||||
public abstract Beast copy() throws CloneNotSupportedException;
|
||||
public abstract Beast copy();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return getClass() == obj.getClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,11 +35,12 @@ public class ElfBeast extends Beast {
|
||||
}
|
||||
|
||||
public ElfBeast(ElfBeast elfBeast) {
|
||||
super(elfBeast);
|
||||
this.helpType = elfBeast.helpType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Beast copy() {
|
||||
public ElfBeast copy() {
|
||||
return new ElfBeast(this);
|
||||
}
|
||||
|
||||
@ -48,4 +49,26 @@ public class ElfBeast extends Beast {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -28,7 +28,6 @@ package com.iluwatar.prototype;
|
||||
*/
|
||||
public class ElfMage extends Mage {
|
||||
|
||||
|
||||
private String helpType;
|
||||
|
||||
public ElfMage(String helpType) {
|
||||
@ -36,6 +35,7 @@ public class ElfMage extends Mage {
|
||||
}
|
||||
|
||||
public ElfMage(ElfMage elfMage) {
|
||||
super(elfMage);
|
||||
this.helpType = elfMage.helpType;
|
||||
}
|
||||
|
||||
@ -49,4 +49,25 @@ public class ElfMage extends Mage {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class ElfWarlord extends Warlord {
|
||||
}
|
||||
|
||||
public ElfWarlord(ElfWarlord elfWarlord) {
|
||||
super(elfWarlord);
|
||||
this.helpType = elfWarlord.helpType;
|
||||
}
|
||||
|
||||
@ -48,4 +49,25 @@ public class ElfWarlord extends Warlord {
|
||||
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;
|
||||
}
|
||||
}
|
@ -45,33 +45,21 @@ public class HeroFactoryImpl implements HeroFactory {
|
||||
* 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();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -26,9 +26,24 @@ package com.iluwatar.prototype;
|
||||
/**
|
||||
* Mage.
|
||||
*/
|
||||
public abstract class Mage extends Prototype {
|
||||
public abstract class Mage implements Prototype {
|
||||
|
||||
public Mage() { }
|
||||
|
||||
public Mage(Mage source) { }
|
||||
|
||||
@Override
|
||||
public abstract Mage copy() throws CloneNotSupportedException;
|
||||
public abstract Mage copy();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return getClass() == obj.getClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,11 +35,12 @@ public class OrcBeast extends Beast {
|
||||
}
|
||||
|
||||
public OrcBeast(OrcBeast orcBeast) {
|
||||
super(orcBeast);
|
||||
this.weapon = orcBeast.weapon;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Beast copy() {
|
||||
public OrcBeast copy() {
|
||||
return new OrcBeast(this);
|
||||
}
|
||||
|
||||
@ -48,5 +49,27 @@ public class OrcBeast extends Beast {
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class OrcMage extends Mage {
|
||||
}
|
||||
|
||||
public OrcMage(OrcMage orcMage) {
|
||||
super(orcMage);
|
||||
this.weapon = orcMage.weapon;
|
||||
}
|
||||
|
||||
@ -48,4 +49,25 @@ public class OrcMage extends Mage {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -35,6 +35,7 @@ public class OrcWarlord extends Warlord {
|
||||
}
|
||||
|
||||
public OrcWarlord(OrcWarlord orcWarlord) {
|
||||
super(orcWarlord);
|
||||
this.weapon = orcWarlord.weapon;
|
||||
}
|
||||
|
||||
@ -48,4 +49,25 @@ public class OrcWarlord extends Warlord {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -26,8 +26,8 @@ package com.iluwatar.prototype;
|
||||
/**
|
||||
* Prototype.
|
||||
*/
|
||||
public abstract class Prototype implements Cloneable {
|
||||
public interface Prototype {
|
||||
|
||||
public abstract Object copy() throws CloneNotSupportedException;
|
||||
Object copy();
|
||||
|
||||
}
|
||||
|
@ -26,9 +26,24 @@ package com.iluwatar.prototype;
|
||||
/**
|
||||
* Warlord.
|
||||
*/
|
||||
public abstract class Warlord extends Prototype {
|
||||
public abstract class Warlord implements Prototype {
|
||||
|
||||
public Warlord() { }
|
||||
|
||||
public Warlord(Warlord source) { }
|
||||
|
||||
@Override
|
||||
public abstract Warlord copy() throws CloneNotSupportedException;
|
||||
public abstract Warlord copy();
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj) {
|
||||
return true;
|
||||
}
|
||||
if (obj == null) {
|
||||
return false;
|
||||
}
|
||||
return getClass() == obj.getClass();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,62 +0,0 @@
|
||||
/*
|
||||
* The MIT License
|
||||
* Copyright © 2014-2019 Ilkka Seppälä
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
* THE SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.iluwatar.prototype;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Date: 12/28/15 - 8:34 PM
|
||||
*
|
||||
* @author Jeroen Meulemeester
|
||||
*/
|
||||
public class HeroFactoryImplTest {
|
||||
|
||||
@Test
|
||||
public void testFactory() throws Exception {
|
||||
final Mage mage = mock(Mage.class);
|
||||
final Warlord warlord = mock(Warlord.class);
|
||||
final Beast beast = mock(Beast.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).copy();
|
||||
verify(warlord).copy();
|
||||
verify(beast).copy();
|
||||
verifyNoMoreInteractions(mage, warlord, beast);
|
||||
}
|
||||
|
||||
}
|
@ -57,6 +57,7 @@ public class PrototypeTest<P extends Prototype> {
|
||||
assertNotNull(clone);
|
||||
assertNotSame(clone, testedPrototype);
|
||||
assertSame(testedPrototype.getClass(), clone.getClass());
|
||||
assertEquals(clone, testedPrototype);
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user