public interface Prototype {
Object copy();
}
public abstract class Beast implements Prototype {
public Beast() { }
public Beast(Beast source) { }
@Override public abstract Beast copy();
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } return getClass() == obj.getClass(); }
}
public class OrcBeast extends Beast {
private String weapon;
public OrcBeast(String weapon) { this.weapon = weapon; }
public OrcBeast(OrcBeast orcBeast) { super(orcBeast); this.weapon = orcBeast.weapon; }
@Override public OrcBeast copy() { return new OrcBeast(this); }
@Override 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; } var other = (OrcBeast) obj; if (weapon == null) { return other.weapon == null; } return weapon.equals(other.weapon); }
}
public class ElfBeast extends Beast {
private String helpType;
public ElfBeast(String helpType) { this.helpType = helpType; }
public ElfBeast(ElfBeast elfBeast) { super(elfBeast); this.helpType = elfBeast.helpType; }
@Override public ElfBeast copy() { return new ElfBeast(this); }
@Override 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; } var other = (ElfBeast) obj; if (helpType == null) { return other.helpType == null; } return helpType.equals(other.helpType); }
}
public abstract class Warlord implements Prototype {
public Warlord() { }
public Warlord(Warlord source) { }
@Override public abstract Warlord copy();
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } return getClass() == obj.getClass(); }
}
public class OrcWarlord extends Warlord {
private String weapon;
public OrcWarlord(String weapon) { this.weapon = weapon; }
public OrcWarlord(OrcWarlord orcWarlord) { super(orcWarlord); this.weapon = orcWarlord.weapon; }
@Override public OrcWarlord copy() { return new OrcWarlord(this); }
@Override 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; } var other = (OrcWarlord) obj; if (weapon == null) { return other.weapon == null; } return weapon.equals(other.weapon); } }
public class ElfWarlord extends Warlord {
private String helpType;
public ElfWarlord(String helpType) { this.helpType = helpType; }
public ElfWarlord(ElfWarlord elfWarlord) { super(elfWarlord); this.helpType = elfWarlord.helpType; }
@Override public ElfWarlord copy() { return new ElfWarlord(this); }
@Override 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; } var other = (ElfWarlord) obj; if (helpType == null) { return other.helpType == null; } return helpType.equals(other.helpType); } }
public abstract class Mage implements Prototype {
public Mage() { }
public Mage(Mage source) { }
@Override public abstract Mage copy();
@Override public boolean equals(Object obj) { if (this == obj) { return true; } if (obj == null) { return false; } return getClass() == obj.getClass(); }
}
public class OrcMage extends Mage {
private String weapon;
public OrcMage(String weapon) { this.weapon = weapon; }
public OrcMage(OrcMage orcMage) { super(orcMage); this.weapon = orcMage.weapon; }
@Override public OrcMage copy() { return new OrcMage(this); }
@Override 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; } var other = (OrcMage) obj; if (weapon == null) { return other.weapon == null; } return weapon.equals(other.weapon); } }
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; }
@Override public ElfMage copy() { return new ElfMage(this); }
@Override 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; } var other = (ElfMage) obj; if (helpType == null) { return other.helpType == null; } return helpType.equals(other.helpType); } }
public interface HeroFactory {
Mage createMage();
Warlord createWarlord();
Beast createBeast();
}
public class HeroFactoryImpl implements HeroFactory {
private Mage mage; private Warlord warlord; private Beast beast;
public HeroFactoryImpl(Mage mage, Warlord warlord, Beast beast) { this.mage = mage; this.warlord = warlord; this.beast = beast; }
public Mage createMage() { return mage.copy(); }
public Warlord createWarlord() { return warlord.copy(); }
public Beast createBeast() { return beast.copy(); }
}
import org.slf4j.Logger; import org.slf4j.LoggerFactory;
public class App {
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
public static void main(String[] args) { var factory = new HeroFactoryImpl( new ElfMage("cooking"), new ElfWarlord("cleaning"), new ElfBeast("protecting") ); var mage = factory.createMage(); var warlord = factory.createWarlord(); var beast = factory.createBeast(); LOGGER.info(mage.toString()); LOGGER.info(warlord.toString()); LOGGER.info(beast.toString());
factory = new HeroFactoryImpl( new OrcMage("axe"), new OrcWarlord("sword"), new OrcBeast("laser") ); mage = factory.createMage(); warlord = factory.createWarlord(); beast = factory.createBeast(); LOGGER.info(mage.toString()); LOGGER.info(warlord.toString()); LOGGER.info(beast.toString()); } }
|