From bd48d6ce1046c51aaf93ee48f4887ba4b04e89fd Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Fri, 4 Sep 2020 17:31:50 +0300 Subject: [PATCH] refactor --- .../com/iluwatar/abstractfactory/App.java | 82 +++++-------------- .../com/iluwatar/abstractfactory/Kingdom.java | 38 +++++++++ .../abstractfactory/AbstractFactoryTest.java | 47 ++++++----- 3 files changed, 88 insertions(+), 79 deletions(-) create mode 100644 abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java diff --git a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java index 6cdaf865f..71af19b37 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -23,12 +23,8 @@ package com.iluwatar.abstractfactory; -import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; import lombok.Getter; -import lombok.Setter; 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 @@ -45,55 +41,10 @@ import org.slf4j.LoggerFactory; * both concrete implementations to create a king, a castle and an army. */ @Slf4j -public class App { +public class App implements Runnable { - @Setter @Getter - private King king; - - @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."); - } - } - } + private final Kingdom kingdom = new Kingdom(); /** * Program entry point. @@ -101,19 +52,30 @@ public class App { * @param args command line args */ public static void main(String[] args) { - var app = new App(); + } + @Override + public void run() { log.info("Elf Kingdom"); - app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); - log.info(app.getArmy().getDescription()); - log.info(app.getCastle().getDescription()); - log.info(app.getKing().getDescription()); + createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF)); + log.info(kingdom.getArmy().getDescription()); + log.info(kingdom.getCastle().getDescription()); + log.info(kingdom.getKing().getDescription()); log.info("Orc Kingdom"); - app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); - log.info(app.getArmy().getDescription()); - log.info(app.getCastle().getDescription()); - log.info(app.getKing().getDescription()); + createKingdom(Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC)); + log.info(kingdom.getArmy().getDescription()); + log.info(kingdom.getCastle().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()); } } \ No newline at end of file diff --git a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java new file mode 100644 index 000000000..6e24005ab --- /dev/null +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java @@ -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."); + } + } + } +} diff --git a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java index 1506844cf..db6fd04ee 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -23,14 +23,13 @@ package com.iluwatar.abstractfactory; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import com.iluwatar.abstractfactory.App.FactoryMaker; -import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; +import lombok.val; import org.junit.jupiter.api.BeforeEach; 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. */ @@ -42,19 +41,21 @@ public class AbstractFactoryTest { @BeforeEach public void setUp() { - elfFactory = FactoryMaker.makeFactory(KingdomType.ELF); - orcFactory = FactoryMaker.makeFactory(KingdomType.ORC); + elfFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ELF); + orcFactory = Kingdom.FactoryMaker.makeFactory(Kingdom.FactoryMaker.KingdomType.ORC); } @Test public void king() { app.createKingdom(elfFactory); - final var elfKing = app.getKing(); + val kingdom = app.getKingdom(); + + val elfKing = kingdom.getKing(); assertTrue(elfKing instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); app.createKingdom(orcFactory); - final var orcKing = app.getKing(); + val orcKing = kingdom.getKing(); assertTrue(orcKing instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); } @@ -62,12 +63,14 @@ public class AbstractFactoryTest { @Test public void castle() { app.createKingdom(elfFactory); - final var elfCastle = app.getCastle(); + val kingdom = app.getKingdom(); + + val elfCastle = kingdom.getCastle(); assertTrue(elfCastle instanceof ElfCastle); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); app.createKingdom(orcFactory); - final var orcCastle = app.getCastle(); + val orcCastle = kingdom.getCastle(); assertTrue(orcCastle instanceof OrcCastle); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); } @@ -75,12 +78,14 @@ public class AbstractFactoryTest { @Test public void army() { app.createKingdom(elfFactory); - final var elfArmy = app.getArmy(); + val kingdom = app.getKingdom(); + + val elfArmy = kingdom.getArmy(); assertTrue(elfArmy instanceof ElfArmy); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); app.createKingdom(orcFactory); - final var orcArmy = app.getArmy(); + val orcArmy = kingdom.getArmy(); assertTrue(orcArmy instanceof OrcArmy); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); } @@ -88,9 +93,11 @@ public class AbstractFactoryTest { @Test public void createElfKingdom() { app.createKingdom(elfFactory); - final var king = app.getKing(); - final var castle = app.getCastle(); - final var army = app.getArmy(); + val kingdom = app.getKingdom(); + + val king = kingdom.getKing(); + val castle = kingdom.getCastle(); + val army = kingdom.getArmy(); assertTrue(king instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof ElfCastle); @@ -102,9 +109,11 @@ public class AbstractFactoryTest { @Test public void createOrcKingdom() { app.createKingdom(orcFactory); - final var king = app.getKing(); - final var castle = app.getCastle(); - final var army = app.getArmy(); + val kingdom = app.getKingdom(); + + val king = kingdom.getKing(); + val castle = kingdom.getCastle(); + val army = kingdom.getArmy(); assertTrue(king instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof OrcCastle);