From d37922bf823900f9ce319736927dfb2203808ded Mon Sep 17 00:00:00 2001 From: Tschis Date: Tue, 7 Nov 2017 21:05:04 -0300 Subject: [PATCH 1/4] add factory of factories --- .../com/iluwatar/abstractfactory/App.java | 37 ++++++++++++++++--- .../abstractfactory/AbstractFactoryTest.java | 7 +++- 2 files changed, 36 insertions(+), 8 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 d7cb296c0..663018588 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -25,6 +25,8 @@ package com.iluwatar.abstractfactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; + /** * * The Abstract Factory pattern provides a way to encapsulate a group of individual factories that have a common theme @@ -56,7 +58,7 @@ public class App { setCastle(factory.createCastle()); setArmy(factory.createArmy()); } - + King getKing(final KingdomFactory factory) { return factory.createKing(); } @@ -92,6 +94,31 @@ public class App { private void setArmy(final Army army) { this.army = army; } + + /** + * The factory of kingdom factories. + */ + public static class FactoryMaker { + + private FactoryMaker() {} + + public enum KingdomType { + ELF, + ORC + } + + 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 @@ -104,17 +131,15 @@ public class App { App app = new App(); LOGGER.info("Elf Kingdom"); - app.createKingdom(new ElfKingdomFactory()); + app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); LOGGER.info(app.getArmy().getDescription()); LOGGER.info(app.getCastle().getDescription()); LOGGER.info(app.getKing().getDescription()); LOGGER.info("Orc Kingdom"); - app.createKingdom(new OrcKingdomFactory()); + app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); LOGGER.info(app.getArmy().getDescription()); LOGGER.info(app.getCastle().getDescription()); LOGGER.info(app.getKing().getDescription()); - } - -} +} \ 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 b6fca8b23..67ecd43f4 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -28,6 +28,9 @@ import static org.junit.Assert.assertTrue; import org.junit.Before; import org.junit.Test; +import com.iluwatar.abstractfactory.App.FactoryMaker; +import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; + /** * Test for abstract factory */ @@ -39,8 +42,8 @@ public class AbstractFactoryTest { @Before public void setUp() { - elfFactory = new ElfKingdomFactory(); - orcFactory = new OrcKingdomFactory(); + elfFactory = FactoryMaker.makeFactory(KingdomType.ELF); + orcFactory = FactoryMaker.makeFactory(KingdomType.ORC); } @Test From 3634b3338cc2fab6cfa147c9475b986165471b73 Mon Sep 17 00:00:00 2001 From: Tschis Date: Tue, 7 Nov 2017 22:19:10 -0300 Subject: [PATCH 2/4] Fix checkstyle validations --- .../com/iluwatar/abstractfactory/App.java | 45 +++++++++---------- .../abstractfactory/AbstractFactoryTest.java | 6 +-- 2 files changed, 25 insertions(+), 26 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 663018588..4cc1bcc0d 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -95,33 +95,32 @@ public class App { this.army = army; } - /** - * The factory of kingdom factories. - */ public static class FactoryMaker { - - private FactoryMaker() {} - - public enum KingdomType { - ELF, - ORC - } - - 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 FactoryMaker() { + } + + public enum KingdomType { + ELF, ORC + } + + /** + * The factory of kingdom factories. + */ + 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. * * @param args * command line args 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 67ecd43f4..7613edf65 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -25,12 +25,12 @@ package com.iluwatar.abstractfactory; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; -import org.junit.Before; -import org.junit.Test; - import com.iluwatar.abstractfactory.App.FactoryMaker; import com.iluwatar.abstractfactory.App.FactoryMaker.KingdomType; +import org.junit.Before; +import org.junit.Test; + /** * Test for abstract factory */ From 933c84ff1c82ee4c0e3faa147f6160380e2bb817 Mon Sep 17 00:00:00 2001 From: Tschis Date: Tue, 7 Nov 2017 22:32:36 -0300 Subject: [PATCH 3/4] Fix checkstyle validations --- .../main/java/com/iluwatar/abstractfactory/App.java | 11 +++++++---- 1 file changed, 7 insertions(+), 4 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 4cc1bcc0d..4d6043abc 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -95,17 +95,20 @@ public class App { this.army = army; } + /** + * The factory of kingdom factories. + */ public static class FactoryMaker { - private FactoryMaker() { - } - + /** + * Enumeration for the different types of Kingdoms. + */ public enum KingdomType { ELF, ORC } /** - * The factory of kingdom factories. + * The factory method to create KingdomFactory concrete objects. */ public static KingdomFactory makeFactory(KingdomType type) { switch (type) { From 14279278cd2c57942af6effd2e0126badfc74133 Mon Sep 17 00:00:00 2001 From: Rodolfo Forte Date: Sun, 19 Nov 2017 19:22:03 -0200 Subject: [PATCH 4/4] Sync readme.md with the new code --- abstract-factory/README.md | 39 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/abstract-factory/README.md b/abstract-factory/README.md index c049401fc..0fb3c3cb2 100644 --- a/abstract-factory/README.md +++ b/abstract-factory/README.md @@ -120,6 +120,45 @@ king.getDescription(); // Output: This is the Elven king! army.getDescription(); // Output: This is the Elven Army! ``` +Now, we can design a factory for our different kingdom factories. In this example, we created FactoryMaker, responsible for returning an instance of either ElfKingdomFactory or OrcKingdomFactory. +The client can use FactoryMaker to create the desired concrete factory which, in turn, will produce different concrete objects (Army, King, Castle). +In this example, we also used an enum to parameterize which type of kingdom factory the client will ask for. + +``` +public static class FactoryMaker { + + public enum KingdomType { + ELF, ORC + } + + 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."); + } + } +} + +public static void main(String[] args) { + App app = new App(); + + LOGGER.info("Elf Kingdom"); + app.createKingdom(FactoryMaker.makeFactory(KingdomType.ELF)); + LOGGER.info(app.getArmy().getDescription()); + LOGGER.info(app.getCastle().getDescription()); + LOGGER.info(app.getKing().getDescription()); + + LOGGER.info("Orc Kingdom"); + app.createKingdom(FactoryMaker.makeFactory(KingdomType.ORC)); + -- similar use of the orc factory +} +``` + + ## Applicability Use the Abstract Factory pattern when