From 902e541f1ad121ecd58bac0d3c9896b349b08367 Mon Sep 17 00:00:00 2001 From: hbothra Date: Thu, 17 Oct 2019 10:54:58 +0530 Subject: [PATCH 1/5] Using static object to reduce memory foot prints --- .../com/iluwatar/factory/method/ElfBlacksmith.java | 13 +++++++++++-- .../com/iluwatar/factory/method/OrcBlacksmith.java | 11 ++++++++++- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java index 9bc79c2c4e68..b85da9927e4a 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java @@ -22,6 +22,9 @@ */ package com.iluwatar.factory.method; +import java.util.HashMap; +import java.util.Map; + /** * * Concrete subclass for creating new objects. @@ -29,9 +32,15 @@ */ public class ElfBlacksmith implements Blacksmith { + private static Map weaponProtoType = new HashMap<>(WeaponType.values().length); + { + for (WeaponType type : WeaponType.values()) { + weaponProtoType.put(type, new ElfWeapon(type)); + } + } @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return new ElfWeapon(weaponType); + return weaponProtoType.get(weaponType); } - + } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java index adb6e4fff17b..767f40d3558d 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java @@ -22,6 +22,9 @@ */ package com.iluwatar.factory.method; +import java.util.HashMap; +import java.util.Map; + /** * * Concrete subclass for creating new objects. @@ -29,8 +32,14 @@ */ public class OrcBlacksmith implements Blacksmith { + private static Map weaponProtoType = new HashMap<>(WeaponType.values().length); + { + for (WeaponType type : WeaponType.values()) { + weaponProtoType.put(type, new OrcWeapon(type)); + } + } @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return new OrcWeapon(weaponType); + return weaponProtoType.get(weaponType); } } From 8af42a1f0ca59cc66352c172515e6e0175196ffd Mon Sep 17 00:00:00 2001 From: hbothra Date: Thu, 17 Oct 2019 11:05:08 +0530 Subject: [PATCH 2/5] Updating README along with name of static fields --- factory-method/README.md | 4 ++-- .../java/com/iluwatar/factory/method/ElfBlacksmith.java | 6 +++--- .../java/com/iluwatar/factory/method/OrcBlacksmith.java | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/factory-method/README.md b/factory-method/README.md index 087221fb9dfe..14ec558d0aa2 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -42,13 +42,13 @@ public interface Blacksmith { public class ElfBlacksmith implements Blacksmith { public Weapon manufactureWeapon(WeaponType weaponType) { - return new ElfWeapon(weaponType); + return ELF_ARSENAL.get(weaponType); } } public class OrcBlacksmith implements Blacksmith { public Weapon manufactureWeapon(WeaponType weaponType) { - return new OrcWeapon(weaponType); + return ORC_ARSENAL.get(weaponType); } } ``` diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java index b85da9927e4a..8e22bae145de 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java @@ -32,15 +32,15 @@ */ public class ElfBlacksmith implements Blacksmith { - private static Map weaponProtoType = new HashMap<>(WeaponType.values().length); + private static Map ELF_ARSENAL = new HashMap<>(WeaponType.values().length); { for (WeaponType type : WeaponType.values()) { - weaponProtoType.put(type, new ElfWeapon(type)); + ELF_ARSENAL.put(type, new ElfWeapon(type)); } } @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return weaponProtoType.get(weaponType); + return ELF_ARSENAL.get(weaponType); } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java index 767f40d3558d..177c2f91494e 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java @@ -32,14 +32,14 @@ */ public class OrcBlacksmith implements Blacksmith { - private static Map weaponProtoType = new HashMap<>(WeaponType.values().length); + private static Map ORC_ARSENAL = new HashMap<>(WeaponType.values().length); { for (WeaponType type : WeaponType.values()) { - weaponProtoType.put(type, new OrcWeapon(type)); + ORC_ARSENAL.put(type, new OrcWeapon(type)); } } @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return weaponProtoType.get(weaponType); + return ORC_ARSENAL.get(weaponType); } } From d18a98ec9c6a15f04333de2ee17f4437cdbd044d Mon Sep 17 00:00:00 2001 From: hbothra Date: Sat, 26 Oct 2019 09:33:23 +0530 Subject: [PATCH 3/5] Updating code as per review comments --- .../com/iluwatar/factory/method/ElfBlacksmith.java | 14 ++++++++------ .../com/iluwatar/factory/method/OrcBlacksmith.java | 14 ++++++++------ .../iluwatar/factory/method/FactoryMethodTest.java | 2 ++ 3 files changed, 18 insertions(+), 12 deletions(-) diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java index 1292f5438c4a..eccff10d924c 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java @@ -33,15 +33,17 @@ */ public class ElfBlacksmith implements Blacksmith { - private static Map ELF_ARSENAL = new HashMap<>(WeaponType.values().length); - { - for (WeaponType type : WeaponType.values()) { - ELF_ARSENAL.put(type, new ElfWeapon(type)); - } + private static Map ELFARSENAL; + static { + ELFARSENAL= new HashMap<>(WeaponType.values().length); + for (WeaponType type : WeaponType.values()) { + ELFARSENAL.put(type, new ElfWeapon(type)); + } } + @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return ELF_ARSENAL.get(weaponType); + return ELFARSENAL.get(weaponType); } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java index 8f350b3bc4ed..6736630492c6 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java @@ -33,14 +33,16 @@ */ public class OrcBlacksmith implements Blacksmith { - private static Map ORC_ARSENAL = new HashMap<>(WeaponType.values().length); - { - for (WeaponType type : WeaponType.values()) { - ORC_ARSENAL.put(type, new OrcWeapon(type)); - } + private static Map ORCARSENAL; + static { + ORCARSENAL= new HashMap<>(WeaponType.values().length); + for (WeaponType type : WeaponType.values()) { + ORCARSENAL.put(type, new OrcWeapon(type)); + } } + @Override public Weapon manufactureWeapon(WeaponType weaponType) { - return ORC_ARSENAL.get(weaponType); + return ORCARSENAL.get(weaponType); } } 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 eab890002f24..bd2e721ed3ab 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 @@ -95,6 +95,8 @@ public void testElfBlacksmithWithSpear() { * @param clazz expected class of the weapon */ private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { + System.out.println(clazz); + System.out.println(weapon); assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName()); assertEquals(expectedWeaponType, weapon.getWeaponType(), "Weapon must be of weaponType: " + expectedWeaponType); } From afb1bce886cc31baa6ed3901a93fde4bb0c02eb0 Mon Sep 17 00:00:00 2001 From: hbothra Date: Wed, 30 Oct 2019 07:25:13 +0530 Subject: [PATCH 4/5] Updating code as per review comments --- .../java/com/iluwatar/factory/method/FactoryMethodTest.java | 2 -- 1 file changed, 2 deletions(-) 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 bd2e721ed3ab..eab890002f24 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 @@ -95,8 +95,6 @@ public void testElfBlacksmithWithSpear() { * @param clazz expected class of the weapon */ private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { - System.out.println(clazz); - System.out.println(weapon); assertTrue(clazz.isInstance(weapon), "Weapon must be an object of: " + clazz.getName()); assertEquals(expectedWeaponType, weapon.getWeaponType(), "Weapon must be of weaponType: " + expectedWeaponType); } From e92028e842562b026d75d5d8ac1ccdefaec8e5ba Mon Sep 17 00:00:00 2001 From: hbothra Date: Wed, 30 Oct 2019 07:26:51 +0530 Subject: [PATCH 5/5] Updating doc as per new code --- factory-method/README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/factory-method/README.md b/factory-method/README.md index 14ec558d0aa2..31e2ab98b744 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -42,13 +42,13 @@ public interface Blacksmith { public class ElfBlacksmith implements Blacksmith { public Weapon manufactureWeapon(WeaponType weaponType) { - return ELF_ARSENAL.get(weaponType); + return ELFARSENAL.get(weaponType); } } public class OrcBlacksmith implements Blacksmith { public Weapon manufactureWeapon(WeaponType weaponType) { - return ORC_ARSENAL.get(weaponType); + return ORCARSENAL.get(weaponType); } } ```