diff --git a/FactoryMethodPattern/Blacksmith.java b/FactoryMethodPattern/Blacksmith.java new file mode 100644 index 000000000000..0cbaeafaa580 --- /dev/null +++ b/FactoryMethodPattern/Blacksmith.java @@ -0,0 +1,3 @@ +public interface Blacksmith { + Weapon manufactureWeapon(WeaponType weaponType); +} diff --git a/FactoryMethodPattern/ElfBlacksmith.java b/FactoryMethodPattern/ElfBlacksmith.java new file mode 100644 index 000000000000..46298ed2ee61 --- /dev/null +++ b/FactoryMethodPattern/ElfBlacksmith.java @@ -0,0 +1,17 @@ +import java.util.HashMap; +import java.util.Map; + +public class ElfBlacksmith implements Blacksmith { + + private static final Map ELF_ARSENAL = new HashMap<>(); + + static { + ELF_ARSENAL.put(WeaponType.SPEAR, new Weapon("Elven Spear")); + ELF_ARSENAL.put(WeaponType.AXE, new Weapon("Elven Axe")); + } + + @Override + public Weapon manufactureWeapon(WeaponType weaponType) { + return ELF_ARSENAL.get(weaponType); + } +} diff --git a/FactoryMethodPattern/Main.java b/FactoryMethodPattern/Main.java new file mode 100644 index 000000000000..726df91d3953 --- /dev/null +++ b/FactoryMethodPattern/Main.java @@ -0,0 +1,22 @@ +public class Main { + public static void main(String[] args) { + + Blacksmith blacksmith; + + // Orc weapons + blacksmith = new OrcBlacksmith(); + Weapon weapon1 = blacksmith.manufactureWeapon(WeaponType.SPEAR); + System.out.println("Orc Blacksmith manufactured: " + weapon1.getName()); + + Weapon weapon2 = blacksmith.manufactureWeapon(WeaponType.AXE); + System.out.println("Orc Blacksmith manufactured: " + weapon2.getName()); + + // Elf weapons + blacksmith = new ElfBlacksmith(); + Weapon weapon3 = blacksmith.manufactureWeapon(WeaponType.SPEAR); + System.out.println("Elf Blacksmith manufactured: " + weapon3.getName()); + + Weapon weapon4 = blacksmith.manufactureWeapon(WeaponType.AXE); + System.out.println("Elf Blacksmith manufactured: " + weapon4.getName()); + } +} diff --git a/FactoryMethodPattern/OrcBlacksmith.java b/FactoryMethodPattern/OrcBlacksmith.java new file mode 100644 index 000000000000..e7fcca1d1fa3 --- /dev/null +++ b/FactoryMethodPattern/OrcBlacksmith.java @@ -0,0 +1,17 @@ +import java.util.HashMap; +import java.util.Map; + +public class OrcBlacksmith implements Blacksmith { + + private static final Map ORC_ARSENAL = new HashMap<>(); + + static { + ORC_ARSENAL.put(WeaponType.SPEAR, new Weapon("Orcish Spear")); + ORC_ARSENAL.put(WeaponType.AXE, new Weapon("Orcish Axe")); + } + + @Override + public Weapon manufactureWeapon(WeaponType weaponType) { + return ORC_ARSENAL.get(weaponType); + } +} diff --git a/FactoryMethodPattern/Weapon.java b/FactoryMethodPattern/Weapon.java new file mode 100644 index 000000000000..75523337b7a9 --- /dev/null +++ b/FactoryMethodPattern/Weapon.java @@ -0,0 +1,11 @@ +public class Weapon { + private final String name; + + public Weapon(String name) { + this.name = name; + } + + public String getName() { + return name; + } +} diff --git a/FactoryMethodPattern/WeaponType.java b/FactoryMethodPattern/WeaponType.java new file mode 100644 index 000000000000..2ce3c295e9e2 --- /dev/null +++ b/FactoryMethodPattern/WeaponType.java @@ -0,0 +1,4 @@ +public enum WeaponType { + SPEAR, + AXE +}