diff --git a/factory-method/README.md b/factory-method/README.md index 17e0818e91d..de3a9dd8c6d 100644 --- a/factory-method/README.md +++ b/factory-method/README.md @@ -19,7 +19,46 @@ Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses. -![alt text](./etc/factory-method_1.png "Factory Method") +## Explanation + +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. + +In plain words +> It provides a way to delegate the instantiation logic to child classes. + +Wikipedia says +> In class-based programming, the factory method pattern is a creational pattern that uses factory methods to deal with the problem of creating objects without having to specify the exact class of the object that will be created. This is done by creating objects by calling a factory method—either specified in an interface and implemented by child classes, or implemented in a base class and optionally overridden by derived classes—rather than by calling a constructor. + + **Programmatic Example** + +Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it + +``` +public interface Blacksmith { + Weapon manufactureWeapon(WeaponType weaponType); +} + +public class ElfBlacksmith implements Blacksmith { + public Weapon manufactureWeapon(WeaponType weaponType) { + return new ElfWeapon(weaponType); + } +} + +public class OrcBlacksmith implements Blacksmith { + public Weapon manufactureWeapon(WeaponType weaponType) { + return new OrcWeapon(weaponType); + } +} +``` + +Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured +``` +Blacksmith blacksmith = new ElfBlacksmith(); +blacksmith.manufactureWeapon(WeaponType.SPEAR); +blacksmith.manufactureWeapon(WeaponType.AXE); +// Elvish weapons are created +``` ## Applicability Use the Factory Method pattern when diff --git a/factory-method/etc/factory-method.png b/factory-method/etc/factory-method.png deleted file mode 100644 index c2edfd6484e..00000000000 Binary files a/factory-method/etc/factory-method.png and /dev/null differ diff --git a/factory-method/etc/factory-method.ucls b/factory-method/etc/factory-method.ucls deleted file mode 100644 index 562437995a6..00000000000 --- a/factory-method/etc/factory-method.ucls +++ /dev/null @@ -1,117 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/factory-method/etc/factory-method.urm.puml b/factory-method/etc/factory-method.urm.puml deleted file mode 100644 index 73a05e17866..00000000000 --- a/factory-method/etc/factory-method.urm.puml +++ /dev/null @@ -1,54 +0,0 @@ -@startuml -package com.iluwatar.factory.method { - class App { - - LOGGER : Logger {static} - - blacksmith : Blacksmith - + App(blacksmith : Blacksmith) - + main(args : String[]) {static} - - manufactureWeapons() - } - interface Blacksmith { - + manufactureWeapon(WeaponType) : Weapon {abstract} - } - class ElfBlacksmith { - + ElfBlacksmith() - + manufactureWeapon(weaponType : WeaponType) : Weapon - } - class ElfWeapon { - - weaponType : WeaponType - + ElfWeapon(weaponType : WeaponType) - + getWeaponType() : WeaponType - + toString() : String - } - class OrcBlacksmith { - + OrcBlacksmith() - + manufactureWeapon(weaponType : WeaponType) : Weapon - } - class OrcWeapon { - - weaponType : WeaponType - + OrcWeapon(weaponType : WeaponType) - + getWeaponType() : WeaponType - + toString() : String - } - interface Weapon { - + getWeaponType() : WeaponType {abstract} - } - enum WeaponType { - + AXE {static} - + SHORT_SWORD {static} - + SPEAR {static} - + UNDEFINED {static} - - title : String - + toString() : String - + valueOf(name : String) : WeaponType {static} - + values() : WeaponType[] {static} - } -} -ElfWeapon --> "-weaponType" WeaponType -OrcWeapon --> "-weaponType" WeaponType -App --> "-blacksmith" Blacksmith -ElfBlacksmith ..|> Blacksmith -ElfWeapon ..|> Weapon -OrcBlacksmith ..|> Blacksmith -OrcWeapon ..|> Weapon -@enduml \ No newline at end of file diff --git a/factory-method/etc/factory-method_1.png b/factory-method/etc/factory-method_1.png deleted file mode 100644 index 6b9276781be..00000000000 Binary files a/factory-method/etc/factory-method_1.png and /dev/null differ diff --git a/pom.xml b/pom.xml index 0ea198c3f1e..7f96043f583 100644 --- a/pom.xml +++ b/pom.xml @@ -461,6 +461,7 @@ java-design-patterns singleton + factory-method