commit
5607a4974c
@ -23,7 +23,6 @@
|
|||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -41,84 +40,14 @@ import org.slf4j.LoggerFactory;
|
|||||||
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
|
* and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses
|
||||||
* both concrete implementations to create a king, a castle and an army.
|
* both concrete implementations to create a king, a castle and an army.
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App implements Runnable {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static Logger log = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
private King king;
|
private final Kingdom kingdom = new Kingdom();
|
||||||
private Castle castle;
|
|
||||||
private Army army;
|
|
||||||
|
|
||||||
/**
|
public Kingdom getKingdom() {
|
||||||
* Creates kingdom.
|
return kingdom;
|
||||||
*/
|
|
||||||
public void createKingdom(final KingdomFactory factory) {
|
|
||||||
setKing(factory.createKing());
|
|
||||||
setCastle(factory.createCastle());
|
|
||||||
setArmy(factory.createArmy());
|
|
||||||
}
|
|
||||||
|
|
||||||
King getKing(final KingdomFactory factory) {
|
|
||||||
return factory.createKing();
|
|
||||||
}
|
|
||||||
|
|
||||||
public King getKing() {
|
|
||||||
return king;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setKing(final King king) {
|
|
||||||
this.king = king;
|
|
||||||
}
|
|
||||||
|
|
||||||
Castle getCastle(final KingdomFactory factory) {
|
|
||||||
return factory.createCastle();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Castle getCastle() {
|
|
||||||
return castle;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setCastle(final Castle castle) {
|
|
||||||
this.castle = castle;
|
|
||||||
}
|
|
||||||
|
|
||||||
Army getArmy(final KingdomFactory factory) {
|
|
||||||
return factory.createArmy();
|
|
||||||
}
|
|
||||||
|
|
||||||
public Army getArmy() {
|
|
||||||
return army;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void setArmy(final Army army) {
|
|
||||||
this.army = army;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The factory of kingdom factories.
|
|
||||||
*/
|
|
||||||
public static class FactoryMaker {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Enumeration for the different types of Kingdoms.
|
|
||||||
*/
|
|
||||||
public enum KingdomType {
|
|
||||||
ELF, ORC
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The factory method to create KingdomFactory concrete objects.
|
|
||||||
*/
|
|
||||||
public static KingdomFactory makeFactory(KingdomType type) {
|
|
||||||
switch (type) {
|
|
||||||
case ELF:
|
|
||||||
return new ElfKingdomFactory();
|
|
||||||
case ORC:
|
|
||||||
return new OrcKingdomFactory();
|
|
||||||
default:
|
|
||||||
throw new IllegalArgumentException("KingdomType not supported.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -127,19 +56,33 @@ public class App {
|
|||||||
* @param args command line args
|
* @param args command line args
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
var app = new App();
|
var app = new App();
|
||||||
|
app.run();
|
||||||
|
}
|
||||||
|
|
||||||
LOGGER.info("Elf Kingdom");
|
@Override
|
||||||
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF));
|
public void run() {
|
||||||
LOGGER.info(app.getArmy().getDescription());
|
log.info("Elf Kingdom");
|
||||||
LOGGER.info(app.getCastle().getDescription());
|
createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
|
||||||
LOGGER.info(app.getKing().getDescription());
|
log.info(kingdom.getArmy().getDescription());
|
||||||
|
log.info(kingdom.getCastle().getDescription());
|
||||||
|
log.info(kingdom.getKing().getDescription());
|
||||||
|
|
||||||
LOGGER.info("Orc Kingdom");
|
log.info("Orc Kingdom");
|
||||||
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC));
|
createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
|
||||||
LOGGER.info(app.getArmy().getDescription());
|
log.info(kingdom.getArmy().getDescription());
|
||||||
LOGGER.info(app.getCastle().getDescription());
|
log.info(kingdom.getCastle().getDescription());
|
||||||
LOGGER.info(app.getKing().getDescription());
|
log.info(kingdom.getKing().getDescription());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates kingdom.
|
||||||
|
* @param kingdomType type of Kingdom
|
||||||
|
*/
|
||||||
|
public void createKingdom(final Kingdom.FactoryMaker.KingdomType kingdomType) {
|
||||||
|
final KingdomFactory kingdomFactory = Kingdom.FactoryMaker.makeFactory(kingdomType);
|
||||||
|
kingdom.setKing(kingdomFactory.createKing());
|
||||||
|
kingdom.setCastle(kingdomFactory.createCastle());
|
||||||
|
kingdom.setArmy(kingdomFactory.createArmy());
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -0,0 +1,59 @@
|
|||||||
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
|
public class Kingdom {
|
||||||
|
|
||||||
|
private King king;
|
||||||
|
private Castle castle;
|
||||||
|
private Army army;
|
||||||
|
|
||||||
|
public King getKing() {
|
||||||
|
return king;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Castle getCastle() {
|
||||||
|
return castle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Army getArmy() {
|
||||||
|
return army;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setKing(King king) {
|
||||||
|
this.king = king;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCastle(Castle castle) {
|
||||||
|
this.castle = castle;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setArmy(Army army) {
|
||||||
|
this.army = army;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The factory of kingdom factories.
|
||||||
|
*/
|
||||||
|
public static class FactoryMaker {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Enumeration for the different types of Kingdoms.
|
||||||
|
*/
|
||||||
|
public enum KingdomType {
|
||||||
|
ELF, ORC
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The factory method to create KingdomFactory concrete objects.
|
||||||
|
*/
|
||||||
|
public static KingdomFactory makeFactory(KingdomType type) {
|
||||||
|
switch (type) {
|
||||||
|
case ELF:
|
||||||
|
return new ElfKingdomFactory();
|
||||||
|
case ORC:
|
||||||
|
return new OrcKingdomFactory();
|
||||||
|
default:
|
||||||
|
throw new IllegalArgumentException("KingdomType not supported.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -23,65 +23,71 @@
|
|||||||
|
|
||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker;
|
|
||||||
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
|
||||||
import org.junit.jupiter.api.Test;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Test for abstract factory.
|
* Test for abstract factory.
|
||||||
*/
|
*/
|
||||||
public class AbstractFactoryTest {
|
public class AbstractFactoryTest {
|
||||||
|
|
||||||
private final App app = new App();
|
private final App app = new App();
|
||||||
private KingdomFactory elfFactory;
|
|
||||||
private KingdomFactory orcFactory;
|
|
||||||
|
|
||||||
@BeforeEach
|
|
||||||
public void setUp() {
|
|
||||||
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF);
|
|
||||||
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void king() {
|
public void king() {
|
||||||
final var elfKing = app.getKing(elfFactory);
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
|
||||||
|
final var kingdom = app.getKingdom();
|
||||||
|
|
||||||
|
final var elfKing = kingdom.getKing();
|
||||||
assertTrue(elfKing instanceof ElfKing);
|
assertTrue(elfKing instanceof ElfKing);
|
||||||
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
|
||||||
final var orcKing = app.getKing(orcFactory);
|
|
||||||
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
|
||||||
|
final var orcKing = kingdom.getKing();
|
||||||
assertTrue(orcKing instanceof OrcKing);
|
assertTrue(orcKing instanceof OrcKing);
|
||||||
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void castle() {
|
public void castle() {
|
||||||
final var elfCastle = app.getCastle(elfFactory);
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
|
||||||
|
final var kingdom = app.getKingdom();
|
||||||
|
|
||||||
|
final var elfCastle = kingdom.getCastle();
|
||||||
assertTrue(elfCastle instanceof ElfCastle);
|
assertTrue(elfCastle instanceof ElfCastle);
|
||||||
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
|
||||||
final var orcCastle = app.getCastle(orcFactory);
|
|
||||||
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
|
||||||
|
final var orcCastle = kingdom.getCastle();
|
||||||
assertTrue(orcCastle instanceof OrcCastle);
|
assertTrue(orcCastle instanceof OrcCastle);
|
||||||
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void army() {
|
public void army() {
|
||||||
final var elfArmy = app.getArmy(elfFactory);
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
|
||||||
|
final var kingdom = app.getKingdom();
|
||||||
|
|
||||||
|
final var elfArmy = kingdom.getArmy();
|
||||||
assertTrue(elfArmy instanceof ElfArmy);
|
assertTrue(elfArmy instanceof ElfArmy);
|
||||||
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
|
||||||
final var orcArmy = app.getArmy(orcFactory);
|
|
||||||
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
|
||||||
|
final var orcArmy = kingdom.getArmy();
|
||||||
assertTrue(orcArmy instanceof OrcArmy);
|
assertTrue(orcArmy instanceof OrcArmy);
|
||||||
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createElfKingdom() {
|
public void createElfKingdom() {
|
||||||
app.createKingdom(elfFactory);
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
|
||||||
final var king = app.getKing();
|
final var kingdom = app.getKingdom();
|
||||||
final var castle = app.getCastle();
|
|
||||||
final var army = app.getArmy();
|
final var king = kingdom.getKing();
|
||||||
|
final var castle = kingdom.getCastle();
|
||||||
|
final var army = kingdom.getArmy();
|
||||||
assertTrue(king instanceof ElfKing);
|
assertTrue(king instanceof ElfKing);
|
||||||
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
|
||||||
assertTrue(castle instanceof ElfCastle);
|
assertTrue(castle instanceof ElfCastle);
|
||||||
@ -92,10 +98,12 @@ public class AbstractFactoryTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createOrcKingdom() {
|
public void createOrcKingdom() {
|
||||||
app.createKingdom(orcFactory);
|
app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
|
||||||
final var king = app.getKing();
|
final var kingdom = app.getKingdom();
|
||||||
final var castle = app.getCastle();
|
|
||||||
final var army = app.getArmy();
|
final var king = kingdom.getKing();
|
||||||
|
final var castle = kingdom.getCastle();
|
||||||
|
final var army = kingdom.getArmy();
|
||||||
assertTrue(king instanceof OrcKing);
|
assertTrue(king instanceof OrcKing);
|
||||||
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
|
||||||
assertTrue(castle instanceof OrcCastle);
|
assertTrue(castle instanceof OrcCastle);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user