This commit is contained in:
Vladislav Golubinov 2020-09-04 17:31:50 +03:00
parent e89042a782
commit bd48d6ce10
3 changed files with 88 additions and 79 deletions

View File

@ -23,12 +23,8 @@
package com.iluwatar.abstractfactory; package com.iluwatar.abstractfactory;
import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType;
import lombok.Getter; import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** /**
* 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
@ -45,55 +41,10 @@ import org.slf4j.LoggerFactory;
* both concrete implementations to create a king, a castle and an army. * both concrete implementations to create a king, a castle and an army.
*/ */
@Slf4j @Slf4j
public class App { public class App implements Runnable {
@Setter
@Getter @Getter
private King king; private final Kingdom kingdom = new Kingdom();
@Setter
@Getter
private Castle castle;
@Setter
@Getter
private Army army;
/**
* Creates kingdom.
*/
public void createKingdom(final KingdomFactory factory) {
setKing(factory.createKing());
setCastle(factory.createCastle());
setArmy(factory.createArmy());
}
/**
* 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.");
}
}
}
/** /**
* Program entry point. * Program entry point.
@ -101,19 +52,30 @@ 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();
}
@Override
public void run() {
log.info("Elf Kingdom"); log.info("Elf Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF));
log.info(app.getArmy().getDescription()); log.info(kingdom.getArmy().getDescription());
log.info(app.getCastle().getDescription()); log.info(kingdom.getCastle().getDescription());
log.info(app.getKing().getDescription()); log.info(kingdom.getKing().getDescription());
log.info("Orc Kingdom"); log.info("Orc Kingdom");
app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC));
log.info(app.getArmy().getDescription()); log.info(kingdom.getArmy().getDescription());
log.info(app.getCastle().getDescription()); log.info(kingdom.getCastle().getDescription());
log.info(app.getKing().getDescription()); log.info(kingdom.getKing().getDescription());
}
/**
* Creates kingdom.
*/
public void createKingdom(final KingdomFactory factory) {
kingdom.setKing(factory.createKing());
kingdom.setCastle(factory.createCastle());
kingdom.setArmy(factory.createArmy());
} }
} }

View File

@ -0,0 +1,38 @@
package com.iluwatar.abstractfactory;
import lombok.Data;
@Data
public class Kingdom {
private King king;
private Castle castle;
private 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.");
}
}
}
}

View File

@ -23,14 +23,13 @@
package com.iluwatar.abstractfactory; package com.iluwatar.abstractfactory;
import static org.junit.jupiter.api.Assertions.assertEquals; import lombok.val;
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.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.assertTrue;
/** /**
* Test for abstract factory. * Test for abstract factory.
*/ */
@ -42,19 +41,21 @@ public class AbstractFactoryTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
elfFactory = FactoryMaker.makeFactory(KingdomType.ELF); elfFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF);
orcFactory = FactoryMaker.makeFactory(KingdomType.ORC); orcFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC);
} }
@Test @Test
public void king() { public void king() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final var elfKing = app.getKing(); val kingdom = app.getKingdom();
val 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(orcFactory);
final var orcKing = app.getKing(); val orcKing = kingdom.getKing();
assertTrue(orcKing instanceof OrcKing); assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
} }
@ -62,12 +63,14 @@ public class AbstractFactoryTest {
@Test @Test
public void castle() { public void castle() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final var elfCastle = app.getCastle(); val kingdom = app.getKingdom();
val 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(orcFactory);
final var orcCastle = app.getCastle(); val orcCastle = kingdom.getCastle();
assertTrue(orcCastle instanceof OrcCastle); assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
} }
@ -75,12 +78,14 @@ public class AbstractFactoryTest {
@Test @Test
public void army() { public void army() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final var elfArmy = app.getArmy(); val kingdom = app.getKingdom();
val 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(orcFactory);
final var orcArmy = app.getArmy(); val orcArmy = kingdom.getArmy();
assertTrue(orcArmy instanceof OrcArmy); assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
} }
@ -88,9 +93,11 @@ public class AbstractFactoryTest {
@Test @Test
public void createElfKingdom() { public void createElfKingdom() {
app.createKingdom(elfFactory); app.createKingdom(elfFactory);
final var king = app.getKing(); val kingdom = app.getKingdom();
final var castle = app.getCastle();
final var army = app.getArmy(); val king = kingdom.getKing();
val castle = kingdom.getCastle();
val 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);
@ -102,9 +109,11 @@ public class AbstractFactoryTest {
@Test @Test
public void createOrcKingdom() { public void createOrcKingdom() {
app.createKingdom(orcFactory); app.createKingdom(orcFactory);
final var king = app.getKing(); val kingdom = app.getKingdom();
final var castle = app.getCastle();
final var army = app.getArmy(); val king = kingdom.getKing();
val castle = kingdom.getCastle();
val 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);