#590 Alter Factory Method presentation
This commit is contained in:
parent
002774b5aa
commit
2d750dc0fd
@ -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.
|
||||
|
||||

|
||||
## 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
|
||||
|
Binary file not shown.
Before Width: | Height: | Size: 22 KiB |
@ -1,117 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<class id="1" language="java" name="com.iluwatar.factory.method.OrcBlacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/OrcBlacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="280" x="-46" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="2" language="java" name="com.iluwatar.factory.method.Blacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/Blacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="88" width="280" x="114" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<enumeration id="3" language="java" name="com.iluwatar.factory.method.WeaponType" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/WeaponType.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="196" width="210" x="793" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</enumeration>
|
||||
<class id="4" language="java" name="com.iluwatar.factory.method.OrcWeapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/OrcWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="178" x="806" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<class id="5" language="java" name="com.iluwatar.factory.method.ElfWeapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/ElfWeapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="172" x="594" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<interface id="6" language="java" name="com.iluwatar.factory.method.Weapon" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/Weapon.java" binary="false" corner="BOTTOM_RIGHT">
|
||||
<position height="70" width="159" x="594" y="385"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</interface>
|
||||
<class id="7" language="java" name="com.iluwatar.factory.method.ElfBlacksmith" project="factory-method"
|
||||
file="/factory-method/src/main/java/com/iluwatar/factory/method/ElfBlacksmith.java" binary="false"
|
||||
corner="BOTTOM_RIGHT">
|
||||
<position height="106" width="280" x="274" y="239"/>
|
||||
<display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</display>
|
||||
</class>
|
||||
<realization id="8">
|
||||
<end type="SOURCE" refId="4"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<realization id="9">
|
||||
<end type="SOURCE" refId="7"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<realization id="10">
|
||||
<end type="SOURCE" refId="5"/>
|
||||
<end type="TARGET" refId="6"/>
|
||||
</realization>
|
||||
<association id="11">
|
||||
<end type="SOURCE" refId="4" navigable="false">
|
||||
<attribute id="12" name="weaponType">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="13" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<realization id="14">
|
||||
<end type="SOURCE" refId="1"/>
|
||||
<end type="TARGET" refId="2"/>
|
||||
</realization>
|
||||
<association id="15">
|
||||
<end type="SOURCE" refId="5" navigable="false">
|
||||
<attribute id="16" name="weaponType">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</attribute>
|
||||
<multiplicity id="17" minimum="0" maximum="1">
|
||||
<position height="0" width="0" x="0" y="0"/>
|
||||
</multiplicity>
|
||||
</end>
|
||||
<end type="TARGET" refId="3" navigable="true"/>
|
||||
<display labels="true" multiplicity="true"/>
|
||||
</association>
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
@ -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
|
Binary file not shown.
Before Width: | Height: | Size: 46 KiB |
Loading…
x
Reference in New Issue
Block a user