From fb890e80ddfd8ac5f4f8be4e84140b2eb1f61015 Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Thu, 3 Sep 2020 20:02:52 +0300 Subject: [PATCH 1/7] refactor --- .../com/iluwatar/abstractfactory/App.java | 12 ----------- .../abstractfactory/AbstractFactoryTest.java | 21 +++++++++++++------ 2 files changed, 15 insertions(+), 18 deletions(-) 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 e158ece74..21b4af305 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -58,10 +58,6 @@ public class App { setArmy(factory.createArmy()); } - King getKing(final KingdomFactory factory) { - return factory.createKing(); - } - public King getKing() { return king; } @@ -70,10 +66,6 @@ public class App { this.king = king; } - Castle getCastle(final KingdomFactory factory) { - return factory.createCastle(); - } - public Castle getCastle() { return castle; } @@ -82,10 +74,6 @@ public class App { this.castle = castle; } - Army getArmy(final KingdomFactory factory) { - return factory.createArmy(); - } - public Army getArmy() { return army; } 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 f3db525a1..1506844cf 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -48,30 +48,39 @@ public class AbstractFactoryTest { @Test public void king() { - final var elfKing = app.getKing(elfFactory); + app.createKingdom(elfFactory); + final var elfKing = app.getKing(); assertTrue(elfKing instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); - final var orcKing = app.getKing(orcFactory); + + app.createKingdom(orcFactory); + final var orcKing = app.getKing(); assertTrue(orcKing instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); } @Test public void castle() { - final var elfCastle = app.getCastle(elfFactory); + app.createKingdom(elfFactory); + final var elfCastle = app.getCastle(); assertTrue(elfCastle instanceof ElfCastle); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); - final var orcCastle = app.getCastle(orcFactory); + + app.createKingdom(orcFactory); + final var orcCastle = app.getCastle(); assertTrue(orcCastle instanceof OrcCastle); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); } @Test public void army() { - final var elfArmy = app.getArmy(elfFactory); + app.createKingdom(elfFactory); + final var elfArmy = app.getArmy(); assertTrue(elfArmy instanceof ElfArmy); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); - final var orcArmy = app.getArmy(orcFactory); + + app.createKingdom(orcFactory); + final var orcArmy = app.getArmy(); assertTrue(orcArmy instanceof OrcArmy); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); } From e89042a782b55202c1945e445df4f73d2a09a2b4 Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Thu, 3 Sep 2020 20:04:29 +0300 Subject: [PATCH 2/7] remove boilerplate code --- abstract-factory/pom.xml | 5 ++ .../com/iluwatar/abstractfactory/App.java | 54 +++++++------------ 2 files changed, 25 insertions(+), 34 deletions(-) diff --git a/abstract-factory/pom.xml b/abstract-factory/pom.xml index 2328c60a8..e10a9436f 100644 --- a/abstract-factory/pom.xml +++ b/abstract-factory/pom.xml @@ -31,6 +31,11 @@ junit-jupiter-engine test + + org.projectlombok + lombok + 1.18.12 + 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 21b4af305..6cdaf865f 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -24,6 +24,9 @@ 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; @@ -41,12 +44,19 @@ import org.slf4j.LoggerFactory; * and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses * both concrete implementations to create a king, a castle and an army. */ +@Slf4j public class App { - private static final Logger LOGGER = LoggerFactory.getLogger(App.class); - + @Setter + @Getter private King king; + + @Setter + @Getter private Castle castle; + + @Setter + @Getter private Army army; /** @@ -58,30 +68,6 @@ public class App { setArmy(factory.createArmy()); } - public King getKing() { - return king; - } - - private void setKing(final King king) { - this.king = king; - } - - public Castle getCastle() { - return castle; - } - - private void setCastle(final Castle castle) { - this.castle = castle; - } - - public Army getArmy() { - return army; - } - - private void setArmy(final Army army) { - this.army = army; - } - /** * The factory of kingdom factories. */ @@ -118,16 +104,16 @@ public class App { var app = new App(); - LOGGER.info("Elf Kingdom"); + log.info("Elf Kingdom"); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); - LOGGER.info(app.getArmy().getDescription()); - LOGGER.info(app.getCastle().getDescription()); - LOGGER.info(app.getKing().getDescription()); + log.info(app.getArmy().getDescription()); + log.info(app.getCastle().getDescription()); + log.info(app.getKing().getDescription()); - LOGGER.info("Orc Kingdom"); + log.info("Orc Kingdom"); app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); - LOGGER.info(app.getArmy().getDescription()); - LOGGER.info(app.getCastle().getDescription()); - LOGGER.info(app.getKing().getDescription()); + log.info(app.getArmy().getDescription()); + log.info(app.getCastle().getDescription()); + log.info(app.getKing().getDescription()); } } \ No newline at end of file From bd48d6ce1046c51aaf93ee48f4887ba4b04e89fd Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Fri, 4 Sep 2020 17:31:50 +0300 Subject: [PATCH 3/7] 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); From 2e36a11e24b09f43c0a6983f29191596d50c18e9 Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Sun, 6 Sep 2020 11:42:39 +0300 Subject: [PATCH 4/7] remove lombok, related to #1503 --- abstract-factory/pom.xml | 5 --- .../com/iluwatar/abstractfactory/App.java | 14 +++++--- .../com/iluwatar/abstractfactory/Kingdom.java | 27 ++++++++++++-- .../abstractfactory/AbstractFactoryTest.java | 35 +++++++++---------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/abstract-factory/pom.xml b/abstract-factory/pom.xml index e10a9436f..2328c60a8 100644 --- a/abstract-factory/pom.xml +++ b/abstract-factory/pom.xml @@ -31,11 +31,6 @@ junit-jupiter-engine test - - org.projectlombok - lombok - 1.18.12 - 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 71af19b37..bebf5c894 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -23,8 +23,10 @@ package com.iluwatar.abstractfactory; -import lombok.Getter; -import lombok.extern.slf4j.Slf4j; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.util.List; /** * The Abstract Factory pattern provides a way to encapsulate a group of individual factories that @@ -40,12 +42,16 @@ import lombok.extern.slf4j.Slf4j; * and its implementations ( {@link ElfKingdomFactory}, {@link OrcKingdomFactory}). The example uses * both concrete implementations to create a king, a castle and an army. */ -@Slf4j public class App implements Runnable { - @Getter + private static Logger log = LoggerFactory.getLogger(App.class); + private final Kingdom kingdom = new Kingdom(); + public Kingdom getKingdom() { + return kingdom; + } + /** * Program entry point. * diff --git a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java index 6e24005ab..82fa0b368 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/Kingdom.java @@ -1,14 +1,35 @@ package com.iluwatar.abstractfactory; -import lombok.Data; - -@Data 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. */ 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 db6fd04ee..54c3b68f1 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -23,7 +23,6 @@ package com.iluwatar.abstractfactory; -import lombok.val; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -48,14 +47,14 @@ public class AbstractFactoryTest { @Test public void king() { app.createKingdom(elfFactory); - val kingdom = app.getKingdom(); + final var kingdom = app.getKingdom(); - val elfKing = kingdom.getKing(); + final var elfKing = kingdom.getKing(); assertTrue(elfKing instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); app.createKingdom(orcFactory); - val orcKing = kingdom.getKing(); + final var orcKing = kingdom.getKing(); assertTrue(orcKing instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); } @@ -63,14 +62,14 @@ public class AbstractFactoryTest { @Test public void castle() { app.createKingdom(elfFactory); - val kingdom = app.getKingdom(); + final var kingdom = app.getKingdom(); - val elfCastle = kingdom.getCastle(); + final var elfCastle = kingdom.getCastle(); assertTrue(elfCastle instanceof ElfCastle); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); app.createKingdom(orcFactory); - val orcCastle = kingdom.getCastle(); + final var orcCastle = kingdom.getCastle(); assertTrue(orcCastle instanceof OrcCastle); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); } @@ -78,14 +77,14 @@ public class AbstractFactoryTest { @Test public void army() { app.createKingdom(elfFactory); - val kingdom = app.getKingdom(); + final var kingdom = app.getKingdom(); - val elfArmy = kingdom.getArmy(); + final var elfArmy = kingdom.getArmy(); assertTrue(elfArmy instanceof ElfArmy); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); app.createKingdom(orcFactory); - val orcArmy = kingdom.getArmy(); + final var orcArmy = kingdom.getArmy(); assertTrue(orcArmy instanceof OrcArmy); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); } @@ -93,11 +92,11 @@ public class AbstractFactoryTest { @Test public void createElfKingdom() { app.createKingdom(elfFactory); - val kingdom = app.getKingdom(); + final var kingdom = app.getKingdom(); - val king = kingdom.getKing(); - val castle = kingdom.getCastle(); - val army = kingdom.getArmy(); + final var king = kingdom.getKing(); + final var castle = kingdom.getCastle(); + final var army = kingdom.getArmy(); assertTrue(king instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof ElfCastle); @@ -109,11 +108,11 @@ public class AbstractFactoryTest { @Test public void createOrcKingdom() { app.createKingdom(orcFactory); - val kingdom = app.getKingdom(); + final var kingdom = app.getKingdom(); - val king = kingdom.getKing(); - val castle = kingdom.getCastle(); - val army = kingdom.getArmy(); + final var king = kingdom.getKing(); + final var castle = kingdom.getCastle(); + final var army = kingdom.getArmy(); assertTrue(king instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, king.getDescription()); assertTrue(castle instanceof OrcCastle); From 87cf6b791cefcba99a5894141078464d2805c870 Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Sun, 6 Sep 2020 11:48:40 +0300 Subject: [PATCH 5/7] refactor --- .../com/iluwatar/abstractfactory/App.java | 16 ++++++------ .../abstractfactory/AbstractFactoryTest.java | 25 ++++++------------- 2 files changed, 16 insertions(+), 25 deletions(-) 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 bebf5c894..05aa8d118 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -26,8 +26,6 @@ package com.iluwatar.abstractfactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.List; - /** * 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 @@ -64,13 +62,13 @@ public class App implements Runnable { @Override public void run() { 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.getCastle().getDescription()); log.info(kingdom.getKing().getDescription()); 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.getCastle().getDescription()); log.info(kingdom.getKing().getDescription()); @@ -78,10 +76,12 @@ public class App implements Runnable { /** * Creates kingdom. + * @param kingdomType */ - public void createKingdom(final KingdomFactory factory) { - kingdom.setKing(factory.createKing()); - kingdom.setCastle(factory.createCastle()); - kingdom.setArmy(factory.createArmy()); + 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()); } } \ No newline at end of file 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 54c3b68f1..17c09be88 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -23,7 +23,6 @@ package com.iluwatar.abstractfactory; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -35,25 +34,17 @@ import static org.junit.jupiter.api.Assertions.assertTrue; public class AbstractFactoryTest { 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 public void king() { - app.createKingdom(elfFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF); final var kingdom = app.getKingdom(); final var elfKing = kingdom.getKing(); assertTrue(elfKing instanceof ElfKing); assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription()); - app.createKingdom(orcFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC); final var orcKing = kingdom.getKing(); assertTrue(orcKing instanceof OrcKing); assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription()); @@ -61,14 +52,14 @@ public class AbstractFactoryTest { @Test public void castle() { - app.createKingdom(elfFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF); final var kingdom = app.getKingdom(); final var elfCastle = kingdom.getCastle(); assertTrue(elfCastle instanceof ElfCastle); assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription()); - app.createKingdom(orcFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC); final var orcCastle = kingdom.getCastle(); assertTrue(orcCastle instanceof OrcCastle); assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription()); @@ -76,14 +67,14 @@ public class AbstractFactoryTest { @Test public void army() { - app.createKingdom(elfFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF); final var kingdom = app.getKingdom(); final var elfArmy = kingdom.getArmy(); assertTrue(elfArmy instanceof ElfArmy); assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription()); - app.createKingdom(orcFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC); final var orcArmy = kingdom.getArmy(); assertTrue(orcArmy instanceof OrcArmy); assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription()); @@ -91,7 +82,7 @@ public class AbstractFactoryTest { @Test public void createElfKingdom() { - app.createKingdom(elfFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ELF); final var kingdom = app.getKingdom(); final var king = kingdom.getKing(); @@ -107,7 +98,7 @@ public class AbstractFactoryTest { @Test public void createOrcKingdom() { - app.createKingdom(orcFactory); + app.createKingdom(Kingdom.FactoryMaker.KingdomType.ORC); final var kingdom = app.getKingdom(); final var king = kingdom.getKing(); From 29eecfd0489c79c8c86850039e55b3bff008d377 Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Sun, 6 Sep 2020 11:52:16 +0300 Subject: [PATCH 6/7] forgot to run the App --- .../src/main/java/com/iluwatar/abstractfactory/App.java | 1 + 1 file changed, 1 insertion(+) 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 05aa8d118..2d49768ec 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -57,6 +57,7 @@ public class App implements Runnable { */ public static void main(String[] args) { var app = new App(); + app.run(); } @Override From bab48efd7c24ee74ba4ad599d08b9942814a57ca Mon Sep 17 00:00:00 2001 From: Vladislav Golubinov Date: Sun, 6 Sep 2020 12:01:48 +0300 Subject: [PATCH 7/7] fix style --- .../src/main/java/com/iluwatar/abstractfactory/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 2d49768ec..baf2d7569 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -77,7 +77,7 @@ public class App implements Runnable { /** * Creates kingdom. - * @param kingdomType + * @param kingdomType type of Kingdom */ public void createKingdom(final Kingdom.FactoryMaker.KingdomType kingdomType) { final KingdomFactory kingdomFactory = Kingdom.FactoryMaker.makeFactory(kingdomType);