From 6feca5bd0111c138402c2745f8bbb42950a60acc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ilkka=20Sepp=C3=A4l=C3=A4?= Date: Sun, 13 Jun 2021 18:35:13 +0300 Subject: [PATCH] Update factory method --- factory-method/README.md | 28 ++++++++----- .../java/com/iluwatar/factory/method/App.java | 39 ++++++------------- .../factory/method/ElfBlacksmith.java | 4 ++ .../iluwatar/factory/method/ElfWeapon.java | 2 +- .../factory/method/OrcBlacksmith.java | 5 +++ .../iluwatar/factory/method/OrcWeapon.java | 2 +- 6 files changed, 41 insertions(+), 39 deletions(-) diff --git a/factory-method/README.md b/factory-method/README.md index 1d41d7d1b..1d1cd87f4 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -21,7 +21,7 @@ Factory Method lets a class defer instantiation to subclasses. ## Explanation -Real world example +Real-world example > Blacksmith manufactures weapons. Elves require Elvish weapons and orcs require Orcish weapons. > Depending on the customer at hand the right type of blacksmith is summoned. @@ -40,7 +40,7 @@ Wikipedia says **Programmatic Example** -Taking our blacksmith example above. First of all we have a `Blacksmith` interface and some +Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some implementations for it: ```java @@ -65,15 +65,25 @@ When the customers come, the correct type of blacksmith is summoned and requeste manufactured: ```java -var blacksmith = new ElfBlacksmith(); -blacksmith.manufactureWeapon(WeaponType.SPEAR); -blacksmith.manufactureWeapon(WeaponType.AXE); +Blacksmith blacksmith = new OrcBlacksmith(); +Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); +LOGGER.info("{} manufactured {}", blacksmith, weapon); +weapon = blacksmith.manufactureWeapon(WeaponType.AXE); +LOGGER.info("{} manufactured {}", blacksmith, weapon); + +blacksmith = new ElfBlacksmith(); +weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); +LOGGER.info("{} manufactured {}", blacksmith, weapon); +weapon = blacksmith.manufactureWeapon(WeaponType.AXE); +LOGGER.info("{} manufactured {}", blacksmith, weapon); ``` Program output: -```java -// Elven spear -// Elven axe +``` +The orc blacksmith manufactured an orcish spear +The orc blacksmith manufactured an orcish axe +The elf blacksmith manufactured an elven spear +The elf blacksmith manufactured an elven axe ``` ## Class diagram @@ -89,7 +99,7 @@ Use the Factory Method pattern when: * Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate. -## Real world examples +## Known uses * [java.util.Calendar](http://docs.oracle.com/javase/8/docs/api/java/util/Calendar.html#getInstance--) * [java.util.ResourceBundle](http://docs.oracle.com/javase/8/docs/api/java/util/ResourceBundle.html#getBundle-java.lang.String-) diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/App.java b/factory-method/src/main/java/com/iluwatar/factory/method/App.java index 126114a84..b76a918d1 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/App.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/App.java @@ -26,7 +26,7 @@ package com.iluwatar.factory.method; import lombok.extern.slf4j.Slf4j; /** - * The Factory Method is a creational design pattern which uses factory methods to deal with the + * The Factory Method is a creational design pattern that uses factory methods to deal with the * problem of creating objects without specifying the exact class of object that will be created. * This is done by creating objects via calling a factory method either specified in an interface * and implemented by child classes, or implemented in a base class and optionally overridden by @@ -41,39 +41,22 @@ import lombok.extern.slf4j.Slf4j; @Slf4j public class App { - private final Blacksmith blacksmith; - - /** - * Creates an instance of App which will use blacksmith to manufacture - * the weapons for war. - * App 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. - * * @param args command line args */ public static void main(String[] args) { - // Lets go to war with Orc weapons - var app = new App(new OrcBlacksmith()); - app.manufactureWeapons(); - // Lets go to war with Elf weapons - app = new App(new ElfBlacksmith()); - app.manufactureWeapons(); - } - - private void manufactureWeapons() { - var weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); - LOGGER.info(weapon.toString()); + Blacksmith blacksmith = new OrcBlacksmith(); + Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); + LOGGER.info("{} manufactured {}", blacksmith, weapon); weapon = blacksmith.manufactureWeapon(WeaponType.AXE); - LOGGER.info(weapon.toString()); + LOGGER.info("{} manufactured {}", blacksmith, weapon); + + blacksmith = new ElfBlacksmith(); + weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); + LOGGER.info("{} manufactured {}", blacksmith, weapon); + weapon = blacksmith.manufactureWeapon(WeaponType.AXE); + LOGGER.info("{} manufactured {}", blacksmith, weapon); } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java index d801f43b8..a7bbbd58f 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java @@ -44,4 +44,8 @@ public class ElfBlacksmith implements Blacksmith { return ELFARSENAL.get(weaponType); } + @Override + public String toString() { + return "The elf blacksmith"; + } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java b/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java index 321f24873..0ba2f716b 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java @@ -37,6 +37,6 @@ public class ElfWeapon implements Weapon { @Override public String toString() { - return "Elven " + weaponType; + return "an elven " + weaponType; } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java index 1ef87b86f..139dda7df 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java @@ -43,4 +43,9 @@ public class OrcBlacksmith implements Blacksmith { public Weapon manufactureWeapon(WeaponType weaponType) { return ORCARSENAL.get(weaponType); } + + @Override + public String toString() { + return "The orc blacksmith"; + } } diff --git a/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java b/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java index 560c7aaf7..f87be2b00 100644 --- a/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java +++ b/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java @@ -37,6 +37,6 @@ public class OrcWeapon implements Weapon { @Override public String toString() { - return "Orcish " + weaponType; + return "an orcish " + weaponType; } }