* 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
This commit is contained in:
Hemant Bothra 2019-10-30 11:57:24 +05:30 committed by Ilkka Seppälä
parent a9dfd7e809
commit cdb80b8ddd
3 changed files with 27 additions and 5 deletions

View File

@ -42,13 +42,13 @@ public interface Blacksmith {
public class ElfBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new ElfWeapon(weaponType);
return ELFARSENAL.get(weaponType);
}
}
public class OrcBlacksmith implements Blacksmith {
public Weapon manufactureWeapon(WeaponType weaponType) {
return new OrcWeapon(weaponType);
return ORCARSENAL.get(weaponType);
}
}
```

View File

@ -23,6 +23,9 @@
package com.iluwatar.factory.method;
import java.util.HashMap;
import java.util.Map;
/**
*
* Concrete subclass for creating new objects.
@ -30,9 +33,17 @@ package com.iluwatar.factory.method;
*/
public class ElfBlacksmith implements Blacksmith {
private static Map<WeaponType, ElfWeapon> 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 new ElfWeapon(weaponType);
return ELFARSENAL.get(weaponType);
}
}

View File

@ -23,6 +23,9 @@
package com.iluwatar.factory.method;
import java.util.HashMap;
import java.util.Map;
/**
*
* Concrete subclass for creating new objects.
@ -30,8 +33,16 @@ package com.iluwatar.factory.method;
*/
public class OrcBlacksmith implements Blacksmith {
private static Map<WeaponType, OrcWeapon> 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 new OrcWeapon(weaponType);
return ORCARSENAL.get(weaponType);
}
}