This commit is contained in:
Vladislav Golubinov 2020-09-06 11:48:40 +03:00
parent 2e36a11e24
commit 87cf6b791c
2 changed files with 16 additions and 25 deletions

View File

@ -26,8 +26,6 @@ package com.iluwatar.abstractfactory;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import java.util.List;
/** /**
* The Abstract Factory pattern provides a way to encapsulate a group of individual factories that * The Abstract Factory pattern provides a way to encapsulate a group of individual factories that
* have a common theme without specifying their concrete classes. In normal usage, the client * have a common theme without specifying their concrete classes. In normal usage, the client
@ -64,13 +62,13 @@ public class App implements Runnable {
@Override @Override
public void run() { public void run() {
log.info("Elf Kingdom"); log.info("Elf Kingdom");
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF)); createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
log.info(kingdom.getArmy().getDescription()); log.info(kingdom.getArmy().getDescription());
log.info(kingdom.getCastle().getDescription()); log.info(kingdom.getCastle().getDescription());
log.info(kingdom.getKing().getDescription()); log.info(kingdom.getKing().getDescription());
log.info("Orc Kingdom"); log.info("Orc Kingdom");
createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC)); createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
log.info(kingdom.getArmy().getDescription()); log.info(kingdom.getArmy().getDescription());
log.info(kingdom.getCastle().getDescription()); log.info(kingdom.getCastle().getDescription());
log.info(kingdom.getKing().getDescription()); log.info(kingdom.getKing().getDescription());
@ -78,10 +76,12 @@ public class App implements Runnable {
/** /**
* Creates kingdom. * Creates kingdom.
* @param kingdomType
*/ */
public void createKingdom(final KingdomFactory factory) { public void createKingdom(final Kingdom.FactoryMaker.KingdomType kingdomType) {
kingdom.setKing(factory.createKing()); final KingdomFactory kingdomFactory = Kingdom.FactoryMaker.makeFactory(kingdomType);
kingdom.setCastle(factory.createCastle()); kingdom.setKing(kingdomFactory.createKing());
kingdom.setArmy(factory.createArmy()); kingdom.setCastle(kingdomFactory.createCastle());
kingdom.setArmy(kingdomFactory.createArmy());
} }
} }

View File

@ -23,7 +23,6 @@
package com.iluwatar.abstractfactory; package com.iluwatar.abstractfactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@ -35,25 +34,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
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 = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF);
orcFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC);
}
@Test @Test
public void king() { public void king() {
app.createKingdom(elfFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
final var kingdom = app.getKingdom(); final var kingdom = app.getKingdom();
final var elfKing = kingdom.getKing(); final var elfKing = kingdom.getKing();
assertTrue(elfKing instanceof ElfKing); assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
app.createKingdom(orcFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
final var orcKing = kingdom.getKing(); final var orcKing = kingdom.getKing();
assertTrue(orcKing instanceof OrcKing); assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
@ -61,14 +52,14 @@ public class AbstractFactoryTest {
@Test @Test
public void castle() { public void castle() {
app.createKingdom(elfFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
final var kingdom = app.getKingdom(); final var kingdom = app.getKingdom();
final var elfCastle = kingdom.getCastle(); final var elfCastle = kingdom.getCastle();
assertTrue(elfCastle instanceof ElfCastle); assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
app.createKingdom(orcFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
final var orcCastle = kingdom.getCastle(); final var orcCastle = kingdom.getCastle();
assertTrue(orcCastle instanceof OrcCastle); assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
@ -76,14 +67,14 @@ public class AbstractFactoryTest {
@Test @Test
public void army() { public void army() {
app.createKingdom(elfFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
final var kingdom = app.getKingdom(); final var kingdom = app.getKingdom();
final var elfArmy = kingdom.getArmy(); final var elfArmy = kingdom.getArmy();
assertTrue(elfArmy instanceof ElfArmy); assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
app.createKingdom(orcFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
final var orcArmy = kingdom.getArmy(); final var orcArmy = kingdom.getArmy();
assertTrue(orcArmy instanceof OrcArmy); assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
@ -91,7 +82,7 @@ public class AbstractFactoryTest {
@Test @Test
public void createElfKingdom() { public void createElfKingdom() {
app.createKingdom(elfFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF);
final var kingdom = app.getKingdom(); final var kingdom = app.getKingdom();
final var king = kingdom.getKing(); final var king = kingdom.getKing();
@ -107,7 +98,7 @@ public class AbstractFactoryTest {
@Test @Test
public void createOrcKingdom() { public void createOrcKingdom() {
app.createKingdom(orcFactory); app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC);
final var kingdom = app.getKingdom(); final var kingdom = app.getKingdom();
final var king = kingdom.getKing(); final var king = kingdom.getKing();