Merge pull request #377 from iluwatar/FactoryMethodChanges
Factory and Abstract Factory changes
This commit is contained in:
		| @@ -52,14 +52,6 @@ public class App { | |||||||
|     setArmy(factory.createArmy()); |     setArmy(factory.createArmy()); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   ElfKingdomFactory getElfKingdomFactory() { |  | ||||||
|     return new ElfKingdomFactory(); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   OrcKingdomFactory getOrcKingdomFactory() { |  | ||||||
|     return new OrcKingdomFactory(); |  | ||||||
|   } |  | ||||||
|  |  | ||||||
|   King getKing(final KingdomFactory factory) { |   King getKing(final KingdomFactory factory) { | ||||||
|     return factory.createKing(); |     return factory.createKing(); | ||||||
|   } |   } | ||||||
| @@ -107,17 +99,13 @@ public class App { | |||||||
|     App app = new App(); |     App app = new App(); | ||||||
|  |  | ||||||
|     System.out.println("Elf Kingdom"); |     System.out.println("Elf Kingdom"); | ||||||
|     KingdomFactory elfKingdomFactory; |     app.createKingdom(new ElfKingdomFactory()); | ||||||
|     elfKingdomFactory = app.getElfKingdomFactory(); |  | ||||||
|     app.createKingdom(elfKingdomFactory); |  | ||||||
|     System.out.println(app.getArmy().getDescription()); |     System.out.println(app.getArmy().getDescription()); | ||||||
|     System.out.println(app.getCastle().getDescription()); |     System.out.println(app.getCastle().getDescription()); | ||||||
|     System.out.println(app.getKing().getDescription()); |     System.out.println(app.getKing().getDescription()); | ||||||
|  |  | ||||||
|     System.out.println("\nOrc Kingdom"); |     System.out.println("\nOrc Kingdom"); | ||||||
|     KingdomFactory orcKingdomFactory; |     app.createKingdom(new OrcKingdomFactory()); | ||||||
|     orcKingdomFactory = app.getOrcKingdomFactory(); |  | ||||||
|     app.createKingdom(orcKingdomFactory); |  | ||||||
|     System.out.println(app.getArmy().getDescription()); |     System.out.println(app.getArmy().getDescription()); | ||||||
|     System.out.println(app.getCastle().getDescription()); |     System.out.println(app.getCastle().getDescription()); | ||||||
|     System.out.println(app.getKing().getDescription()); |     System.out.println(app.getKing().getDescription()); | ||||||
|   | |||||||
| @@ -36,8 +36,8 @@ public class AbstractFactoryTest { | |||||||
|  |  | ||||||
|   @Before |   @Before | ||||||
|   public void setUp() { |   public void setUp() { | ||||||
|     elfFactory = app.getElfKingdomFactory(); |     elfFactory = new ElfKingdomFactory(); | ||||||
|     orcFactory = app.getOrcKingdomFactory(); |     orcFactory = new OrcKingdomFactory(); | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   @Test |   @Test | ||||||
|   | |||||||
| @@ -38,25 +38,40 @@ package com.iluwatar.factory.method; | |||||||
|  */ |  */ | ||||||
| public class App { | public class App { | ||||||
|  |  | ||||||
|  |   private final Blacksmith blacksmith; | ||||||
|  |    | ||||||
|  |   /** | ||||||
|  |    * Creates an instance of <code>App</code> which will use <code>blacksmith</code> to manufacture  | ||||||
|  |    * the weapons for war. | ||||||
|  |    * <code>App</code> is unaware which concrete implementation of {@link Blacksmith} it is using. | ||||||
|  |    * The decision of which blacksmith implementation to use may depend on configuration, or | ||||||
|  |    * the type of rival in war. | ||||||
|  |    * @param blacksmith a non-null implementation of blacksmith | ||||||
|  |    */ | ||||||
|  |   public App(Blacksmith blacksmith) { | ||||||
|  |     this.blacksmith = blacksmith; | ||||||
|  |   } | ||||||
|  |    | ||||||
|   /** |   /** | ||||||
|    * Program entry point |    * Program entry point | ||||||
|    *  |    *  | ||||||
|    * @param args command line args |    * @param args command line args | ||||||
|    */ |    */ | ||||||
|   public static void main(String[] args) { |   public static void main(String[] args) { | ||||||
|     Blacksmith blacksmith; |     // Lets go to war with Orc weapons | ||||||
|     Weapon weapon; |     App app = new App(new OrcBlacksmith()); | ||||||
|  |     app.manufactureWeapons(); | ||||||
|      |      | ||||||
|     blacksmith = new OrcBlacksmith(); |     // Lets go to war with Elf weapons | ||||||
|  |     app = new App(new ElfBlacksmith()); | ||||||
|  |     app.manufactureWeapons(); | ||||||
|  |   } | ||||||
|  |    | ||||||
|  |   private void manufactureWeapons() { | ||||||
|  |     Weapon weapon; | ||||||
|     weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); |     weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); | ||||||
|     System.out.println(weapon); |     System.out.println(weapon); | ||||||
|     weapon = blacksmith.manufactureWeapon(WeaponType.AXE); |     weapon = blacksmith.manufactureWeapon(WeaponType.AXE); | ||||||
|     System.out.println(weapon); |     System.out.println(weapon); | ||||||
|  |  | ||||||
|     blacksmith = new ElfBlacksmith(); |  | ||||||
|     weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); |  | ||||||
|     System.out.println(weapon); |  | ||||||
|     weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); |  | ||||||
|     System.out.println(weapon); |  | ||||||
|   } |   } | ||||||
| } | } | ||||||
|   | |||||||
| @@ -93,7 +93,7 @@ public class FactoryMethodTest { | |||||||
|    * @param expectedWeaponType expected WeaponType of the weapon |    * @param expectedWeaponType expected WeaponType of the weapon | ||||||
|    * @param clazz expected class of the weapon |    * @param clazz expected class of the weapon | ||||||
|    */ |    */ | ||||||
|   private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) { |   private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class<?> clazz) { | ||||||
|     assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon)); |     assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon)); | ||||||
|     assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType, |     assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType, | ||||||
|         weapon.getWeaponType()); |         weapon.getWeaponType()); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user