Skip to content

Commit f64ba22

Browse files
committed
1) Removed warning from test case. 2) Made implementation of App more understandable.
1 parent 57be8aa commit f64ba22

2 files changed

Lines changed: 25 additions & 10 deletions

File tree

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
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)