Skip to content

Commit 6feca5b

Browse files
committed
Update factory method
1 parent 588b106 commit 6feca5b

File tree

6 files changed

+40
-38
lines changed

6 files changed

+40
-38
lines changed

factory-method/README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Factory Method lets a class defer instantiation to subclasses.
2121

2222
## Explanation
2323

24-
Real world example
24+
Real-world example
2525

2626
> Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons.
2727
> Depending on the customer at hand the right type of blacksmith is summoned.
@@ -40,7 +40,7 @@ Wikipedia says
4040
4141
**Programmatic Example**
4242

43-
Taking our blacksmith example above. First of all we have a `Blacksmith` interface and some
43+
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some
4444
implementations for it:
4545

4646
```java
@@ -65,15 +65,25 @@ When the customers come, the correct type of blacksmith is summoned and requeste
6565
manufactured:
6666

6767
```java
68-
var blacksmith = new ElfBlacksmith();
69-
blacksmith.manufactureWeapon(WeaponType.SPEAR);
70-
blacksmith.manufactureWeapon(WeaponType.AXE);
68+
Blacksmith blacksmith = new OrcBlacksmith();
69+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
70+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
71+
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
72+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
73+
74+
blacksmith = new ElfBlacksmith();
75+
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
76+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
77+
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
78+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
7179
```
7280

7381
Program output:
74-
```java
75-
// Elven spear
76-
// Elven axe
82+
```
83+
The orc blacksmith manufactured an orcish spear
84+
The orc blacksmith manufactured an orcish axe
85+
The elf blacksmith manufactured an elven spear
86+
The elf blacksmith manufactured an elven axe
7787
```
7888

7989
## Class diagram
@@ -89,7 +99,7 @@ Use the Factory Method pattern when:
8999
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
90100
knowledge of which helper subclass is the delegate.
91101

92-
## Real world examples
102+
## Known uses
93103

94104
* [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--)
95105
* [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-)

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

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import lombok.extern.slf4j.Slf4j;
2727

2828
/**
29-
* The Factory Method is a creational design pattern which uses factory methods to deal with the
29+
* The Factory Method is a creational design pattern that uses factory methods to deal with the
3030
* problem of creating objects without specifying the exact class of object that will be created.
3131
* This is done by creating objects via calling a factory method either specified in an interface
3232
* and implemented by child classes, or implemented in a base class and optionally overridden by
@@ -41,39 +41,22 @@
4141
@Slf4j
4242
public class App {
4343

44-
private final Blacksmith blacksmith;
45-
46-
/**
47-
* Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture
48-
* the weapons for war.
49-
* <code>App</code> is unaware which concrete implementation of {@link Blacksmith} it is using.
50-
* The decision of which blacksmith implementation to use may depend on configuration, or
51-
* the type of rival in war.
52-
* @param blacksmith a non-null implementation of blacksmith
53-
*/
54-
public App(Blacksmith blacksmith) {
55-
this.blacksmith = blacksmith;
56-
}
57-
5844
/**
5945
* Program entry point.
60-
*
6146
* @param args command line args
6247
*/
6348
public static void main(String[] args) {
64-
// Lets go to war with Orc weapons
65-
var app = new App(new OrcBlacksmith());
66-
app.manufactureWeapons();
6749

68-
// Lets go to war with Elf weapons
69-
app = new App(new ElfBlacksmith());
70-
app.manufactureWeapons();
71-
}
50+
Blacksmith blacksmith = new OrcBlacksmith();
51+
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
52+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
53+
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
54+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
7255

73-
private void manufactureWeapons() {
74-
var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
75-
LOGGER.info(weapon.toString());
56+
blacksmith = new ElfBlacksmith();
57+
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
58+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
7659
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
77-
LOGGER.info(weapon.toString());
60+
LOGGER.info("{} manufactured {}", blacksmith, weapon);
7861
}
7962
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,8 @@ public Weapon manufactureWeapon(WeaponType weaponType) {
4444
return ELFARSENAL.get(weaponType);
4545
}
4646

47+
@Override
48+
public String toString() {
49+
return "The elf blacksmith";
50+
}
4751
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public class ElfWeapon implements Weapon {
3737

3838
@Override
3939
public String toString() {
40-
return "Elven " + weaponType;
40+
return "an elven " + weaponType;
4141
}
4242
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,9 @@ public class OrcBlacksmith implements Blacksmith {
4343
public Weapon manufactureWeapon(WeaponType weaponType) {
4444
return ORCARSENAL.get(weaponType);
4545
}
46+
47+
@Override
48+
public String toString() {
49+
return "The orc blacksmith";
50+
}
4651
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,6 @@ public class OrcWeapon implements Weapon {
3737

3838
@Override
3939
public String toString() {
40-
return "Orcish " + weaponType;
40+
return "an orcish " + weaponType;
4141
}
4242
}

0 commit comments

Comments
 (0)