Skip to content

Commit ea81ef7

Browse files
committed
Merge pull request iluwatar#377 from iluwatar/FactoryMethodChanges
Factory and Abstract Factory changes
2 parents dfef28e + cd077fa commit ea81ef7

4 files changed

Lines changed: 29 additions & 26 deletions

File tree

abstract-factory/src/main/java/com/iluwatar/abstractfactory/App.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,6 @@ public void createKingdom(final KingdomFactory factory) {
5252
setArmy(factory.createArmy());
5353
}
5454

55-
ElfKingdomFactory getElfKingdomFactory() {
56-
return new ElfKingdomFactory();
57-
}
58-
59-
OrcKingdomFactory getOrcKingdomFactory() {
60-
return new OrcKingdomFactory();
61-
}
62-
6355
King getKing(final KingdomFactory factory) {
6456
return factory.createKing();
6557
}
@@ -107,17 +99,13 @@ public static void main(String[] args) {
10799
App app = new App();
108100

109101
System.out.println("Elf Kingdom");
110-
KingdomFactory elfKingdomFactory;
111-
elfKingdomFactory = app.getElfKingdomFactory();
112-
app.createKingdom(elfKingdomFactory);
102+
app.createKingdom(new ElfKingdomFactory());
113103
System.out.println(app.getArmy().getDescription());
114104
System.out.println(app.getCastle().getDescription());
115105
System.out.println(app.getKing().getDescription());
116106

117107
System.out.println("\nOrc Kingdom");
118-
KingdomFactory orcKingdomFactory;
119-
orcKingdomFactory = app.getOrcKingdomFactory();
120-
app.createKingdom(orcKingdomFactory);
108+
app.createKingdom(new OrcKingdomFactory());
121109
System.out.println(app.getArmy().getDescription());
122110
System.out.println(app.getCastle().getDescription());
123111
System.out.println(app.getKing().getDescription());

abstract-factory/src/test/java/com/iluwatar/abstractfactory/AbstractFactoryTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ public class AbstractFactoryTest {
3636

3737
@Before
3838
public void setUp() {
39-
elfFactory = app.getElfKingdomFactory();
40-
orcFactory = app.getOrcKingdomFactory();
39+
elfFactory = new ElfKingdomFactory();
40+
orcFactory = new OrcKingdomFactory();
4141
}
4242

4343
@Test

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

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,25 +38,40 @@
3838
*/
3939
public class App {
4040

41+
private final Blacksmith blacksmith;
42+
43+
/**
44+
* Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture
45+
* the weapons for war.
46+
* <code>App</code> is unaware which concrete implementation of {@link Blacksmith} it is using.
47+
* The decision of which blacksmith implementation to use may depend on configuration, or
48+
* the type of rival in war.
49+
* @param blacksmith a non-null implementation of blacksmith
50+
*/
51+
public App(Blacksmith blacksmith) {
52+
this.blacksmith = blacksmith;
53+
}
54+
4155
/**
4256
* Program entry point
4357
*
4458
* @param args command line args
4559
*/
4660
public static void main(String[] args) {
47-
Blacksmith blacksmith;
61+
// Lets go to war with Orc weapons
62+
App app = new App(new OrcBlacksmith());
63+
app.manufactureWeapons();
64+
65+
// Lets go to war with Elf weapons
66+
app = new App(new ElfBlacksmith());
67+
app.manufactureWeapons();
68+
}
69+
70+
private void manufactureWeapons() {
4871
Weapon weapon;
49-
50-
blacksmith = new OrcBlacksmith();
5172
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
5273
System.out.println(weapon);
5374
weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
5475
System.out.println(weapon);
55-
56-
blacksmith = new ElfBlacksmith();
57-
weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
58-
System.out.println(weapon);
59-
weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
60-
System.out.println(weapon);
6176
}
6277
}

factory-method/src/test/java/com/iluwatar/factory/method/FactoryMethodTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testElfBlacksmithWithSpear() {
9393
* @param expectedWeaponType expected WeaponType of the weapon
9494
* @param clazz expected class of the weapon
9595
*/
96-
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) {
96+
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) {
9797
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
9898
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
9999
weapon.getWeaponType());

0 commit comments

Comments
 (0)