Issue 893 (#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
This commit is contained in:
parent
a9dfd7e809
commit
cdb80b8ddd
@ -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);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user