From f64ba22c64cfc2f7c1340b692b25b183722c1489 Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Wed, 10 Feb 2016 17:38:15 +0530 Subject: [PATCH 1/3] 1) Removed warning from test case. 2) Made implementation of App more understandable. --- .../java/com/iluwatar/factory/method/App.java | 33 ++++++++++++++----- .../factory/method/FactoryMethodTest.java | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/App.java b/factory-method/src/main/java/com/iluwatar/factory/method/App.java index 4056f335c..32b9c82f2 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/App.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/App.java @@ -38,25 +38,40 @@ package com.iluwatar.factory.method; */ public class App { + private final Blacksmith blacksmith; + + /** + * Creates an instance of App which will use blacksmith to manufacture + * the weapons for war. + * App is unaware which concrete implementation of {@link Blacksmith} it is using. + * The decision of which blacksmith implementation to use may depend on configuration, or + * the type of rival in war. + * @param blacksmith + */ + public App(Blacksmith blacksmith) { + this.blacksmith = blacksmith; + } + /** * Program entry point * * @param args command line args */ public static void main(String[] args) { - Blacksmith blacksmith; + // Lets go to war with Orc weapons + App app = new App(new OrcBlacksmith()); + app.manufactureWeapons(); + + // Lets go to war with Elf weapons + app = new App(new ElfBlacksmith()); + app.manufactureWeapons(); + } + + private void manufactureWeapons() { Weapon weapon; - - blacksmith = new OrcBlacksmith(); weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); System.out.println(weapon); weapon = blacksmith.manufactureWeapon(WeaponType.AXE); System.out.println(weapon); - - blacksmith = new ElfBlacksmith(); - weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); - System.out.println(weapon); - weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); - System.out.println(weapon); } } diff --git a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java index 40515d4c9..2f8d1c9bb 100644 --- a/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java +++ b/factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java @@ -93,7 +93,7 @@ public class FactoryMethodTest { * @param expectedWeaponType expected WeaponType of the weapon * @param clazz expected class of the weapon */ - private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { + private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon)); assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType, weapon.getWeaponType()); From b5d4445d635ed35127fa4189ef75a2cd301b2370 Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Wed, 10 Feb 2016 17:53:32 +0530 Subject: [PATCH 2/3] Made example App a bit easier to understand --- .../java/com/iluwatar/abstractfactory/App.java | 16 ++-------------- .../abstractfactory/AbstractFactoryTest.java | 4 ++-- 2 files changed, 4 insertions(+), 16 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 15265c0d4..aae396f1d 100644 --- a/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java +++ b/abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java @@ -52,14 +52,6 @@ public class App { setArmy(factory.createArmy()); } - ElfKingdomFactory getElfKingdomFactory() { - return new ElfKingdomFactory(); - } - - OrcKingdomFactory getOrcKingdomFactory() { - return new OrcKingdomFactory(); - } - King getKing(final KingdomFactory factory) { return factory.createKing(); } @@ -107,17 +99,13 @@ public class App { App app = new App(); System.out.println("Elf Kingdom"); - KingdomFactory elfKingdomFactory; - elfKingdomFactory = app.getElfKingdomFactory(); - app.createKingdom(elfKingdomFactory); + app.createKingdom(new ElfKingdomFactory()); System.out.println(app.getArmy().getDescription()); System.out.println(app.getCastle().getDescription()); System.out.println(app.getKing().getDescription()); System.out.println("\nOrc Kingdom"); - KingdomFactory orcKingdomFactory; - orcKingdomFactory = app.getOrcKingdomFactory(); - app.createKingdom(orcKingdomFactory); + app.createKingdom(new OrcKingdomFactory()); System.out.println(app.getArmy().getDescription()); System.out.println(app.getCastle().getDescription()); System.out.println(app.getKing().getDescription()); 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 5eb083deb..216f0443a 100644 --- a/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java +++ b/abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java @@ -36,8 +36,8 @@ public class AbstractFactoryTest { @Before public void setUp() { - elfFactory = app.getElfKingdomFactory(); - orcFactory = app.getOrcKingdomFactory(); + elfFactory = new ElfKingdomFactory(); + orcFactory = new OrcKingdomFactory(); } @Test From 221b71781a1efd3def76484a4a2aed76cb517f4b Mon Sep 17 00:00:00 2001 From: Narendra Pathai Date: Thu, 11 Feb 2016 12:29:35 +0530 Subject: [PATCH 3/3] Resolved checkstyle audit error --- .../src/main/java/com/iluwatar/factory/method/App.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/App.java b/factory-method/src/main/java/com/iluwatar/factory/method/App.java index 32b9c82f2..cd7a6e6e7 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/App.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/App.java @@ -46,7 +46,7 @@ public class App { * App is unaware which concrete implementation of {@link Blacksmith} it is using. * The decision of which blacksmith implementation to use may depend on configuration, or * the type of rival in war. - * @param blacksmith + * @param blacksmith a non-null implementation of blacksmith */ public App(Blacksmith blacksmith) { this.blacksmith = blacksmith;