File tree 3 files changed +27
-5
lines changed
src/main/java/com/iluwatar/factory/method
3 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -42,13 +42,13 @@ public interface Blacksmith {
42
42
43
43
public class ElfBlacksmith implements Blacksmith {
44
44
public Weapon manufactureWeapon (WeaponType weaponType ) {
45
- return new ElfWeapon (weaponType);
45
+ return ELFARSENAL . get (weaponType);
46
46
}
47
47
}
48
48
49
49
public class OrcBlacksmith implements Blacksmith {
50
50
public Weapon manufactureWeapon (WeaponType weaponType ) {
51
- return new OrcWeapon (weaponType);
51
+ return ORCARSENAL . get (weaponType);
52
52
}
53
53
}
54
54
```
Original file line number Diff line number Diff line change 23
23
24
24
package com .iluwatar .factory .method ;
25
25
26
+ import java .util .HashMap ;
27
+ import java .util .Map ;
28
+
26
29
/**
27
30
*
28
31
* Concrete subclass for creating new objects.
29
32
*
30
33
*/
31
34
public class ElfBlacksmith implements Blacksmith {
32
35
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
+
33
44
@ Override
34
45
public Weapon manufactureWeapon (WeaponType weaponType ) {
35
- return new ElfWeapon (weaponType );
46
+ return ELFARSENAL . get (weaponType );
36
47
}
37
-
48
+
38
49
}
Original file line number Diff line number Diff line change 23
23
24
24
package com .iluwatar .factory .method ;
25
25
26
+ import java .util .HashMap ;
27
+ import java .util .Map ;
28
+
26
29
/**
27
30
*
28
31
* Concrete subclass for creating new objects.
29
32
*
30
33
*/
31
34
public class OrcBlacksmith implements Blacksmith {
32
35
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
+
33
44
@ Override
34
45
public Weapon manufactureWeapon (WeaponType weaponType ) {
35
- return new OrcWeapon (weaponType );
46
+ return ORCARSENAL . get (weaponType );
36
47
}
37
48
}
You can’t perform that action at this time.
0 commit comments