Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions FactoryMethodPattern/Blacksmith.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public interface Blacksmith {
Weapon manufactureWeapon(WeaponType weaponType);
}
17 changes: 17 additions & 0 deletions FactoryMethodPattern/ElfBlacksmith.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.HashMap;
import java.util.Map;

public class ElfBlacksmith implements Blacksmith {

private static final Map<WeaponType, Weapon> 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);
}
}
22 changes: 22 additions & 0 deletions FactoryMethodPattern/Main.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
17 changes: 17 additions & 0 deletions FactoryMethodPattern/OrcBlacksmith.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import java.util.HashMap;
import java.util.Map;

public class OrcBlacksmith implements Blacksmith {

private static final Map<WeaponType, Weapon> 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);
}
}
11 changes: 11 additions & 0 deletions FactoryMethodPattern/Weapon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Weapon {
private final String name;

public Weapon(String name) {
this.name = name;
}

public String getName() {
return name;
}
}
4 changes: 4 additions & 0 deletions FactoryMethodPattern/WeaponType.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum WeaponType {
SPEAR,
AXE
}
Loading