|
38 | 38 | */ |
39 | 39 | public class App { |
40 | 40 |
|
| 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 | + |
41 | 55 | /** |
42 | 56 | * Program entry point |
43 | 57 | * |
44 | 58 | * @param args command line args |
45 | 59 | */ |
46 | 60 | 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() { |
48 | 71 | Weapon weapon; |
49 | | - |
50 | | - blacksmith = new OrcBlacksmith(); |
51 | 72 | weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); |
52 | 73 | System.out.println(weapon); |
53 | 74 | weapon = blacksmith.manufactureWeapon(WeaponType.AXE); |
54 | 75 | 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); |
61 | 76 | } |
62 | 77 | } |
0 commit comments