Repository: iluwatar/java-design-patterns
Issue: Abstract Factory implemented with lambdas
Labels: info: help wanted
epic: pattern
type: enhancement

In the Abstract Factory example show how to implement the pattern with Java 8 lambdas.

There is an example in this talk: https://youtu.be/-k2X7guaArU


Issue comments:

Pull request review comments:

Patch that resolved the issue:
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 15265c0d4e8f..aae396f1dc26 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 void createKingdom(final KingdomFactory factory) {
     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 static void main(String[] args) {
     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/factory-method/src/main/java/com/iluwatar/factory/method/App.java b/factory-method/src/main/java/com/iluwatar/factory/method/App.java
index 4056f335c703..cd7a6e6e7045 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 @@
  */
 public class App {
 
+  private final Blacksmith blacksmith;
+  
+  /**
+   * Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture 
+   * the weapons for war.
+   * <code>App</code> 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 a non-null implementation of 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);
   }
 }


Test patch:
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 5eb083deb2ef..216f0443ae1c 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
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 40515d4c940c..2f8d1c9bbe8d 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 void testElfBlacksmithWithSpear() {
    * @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());