Skip to content

Commit cdb80b8

Browse files
hbothra15iluwatar
authored andcommitted
Issue 893 (iluwatar#1014)
* Using static object to reduce memory foot prints * Updating README along with name of static fields * Updating code as per review comments * Updating code as per review comments * Updating doc as per new code
1 parent a9dfd7e commit cdb80b8

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

factory-method/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,13 @@ public interface Blacksmith {
4242

4343
public class ElfBlacksmith implements Blacksmith {
4444
public Weapon manufactureWeapon(WeaponType weaponType) {
45-
return new ElfWeapon(weaponType);
45+
return ELFARSENAL.get(weaponType);
4646
}
4747
}
4848

4949
public class OrcBlacksmith implements Blacksmith {
5050
public Weapon manufactureWeapon(WeaponType weaponType) {
51-
return new OrcWeapon(weaponType);
51+
return ORCARSENAL.get(weaponType);
5252
}
5353
}
5454
```

factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,27 @@
2323

2424
package com.iluwatar.factory.method;
2525

26+
import java.util.HashMap;
27+
import java.util.Map;
28+
2629
/**
2730
*
2831
* Concrete subclass for creating new objects.
2932
*
3033
*/
3134
public class ElfBlacksmith implements Blacksmith {
3235

36+
private static Map<WeaponType, ElfWeapon> ELFARSENAL;
37+
static {
38+
ELFARSENAL= new HashMap<>(WeaponType.values().length);
39+
for (WeaponType type : WeaponType.values()) {
40+
ELFARSENAL.put(type, new ElfWeapon(type));
41+
}
42+
}
43+
3344
@Override
3445
public Weapon manufactureWeapon(WeaponType weaponType) {
35-
return new ElfWeapon(weaponType);
46+
return ELFARSENAL.get(weaponType);
3647
}
37-
48+
3849
}

factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,26 @@
2323

2424
package com.iluwatar.factory.method;
2525

26+
import java.util.HashMap;
27+
import java.util.Map;
28+
2629
/**
2730
*
2831
* Concrete subclass for creating new objects.
2932
*
3033
*/
3134
public class OrcBlacksmith implements Blacksmith {
3235

36+
private static Map<WeaponType, OrcWeapon> ORCARSENAL;
37+
static {
38+
ORCARSENAL= new HashMap<>(WeaponType.values().length);
39+
for (WeaponType type : WeaponType.values()) {
40+
ORCARSENAL.put(type, new OrcWeapon(type));
41+
}
42+
}
43+
3344
@Override
3445
public Weapon manufactureWeapon(WeaponType weaponType) {
35-
return new OrcWeapon(weaponType);
46+
return ORCARSENAL.get(weaponType);
3647
}
3748
}

0 commit comments

Comments
 (0)