Skip to content

Commit 2d750dc

Browse files
committed
iluwatar#590 Alter Factory Method presentation
1 parent 002774b commit 2d750dc

File tree

6 files changed

+41
-172
lines changed

6 files changed

+41
-172
lines changed

factory-method/README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,46 @@ Define an interface for creating an object, but let subclasses
1919
decide which class to instantiate. Factory Method lets a class defer
2020
instantiation to subclasses.
2121

22-
![alt text](./etc/factory-method_1.png "Factory Method")
22+
## Explanation
23+
24+
Real world example
25+
> 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.
26+
27+
In plain words
28+
> It provides a way to delegate the instantiation logic to child classes.
29+
30+
Wikipedia says
31+
> 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.
32+
33+
**Programmatic Example**
34+
35+
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
36+
37+
```
38+
public interface Blacksmith {
39+
Weapon manufactureWeapon(WeaponType weaponType);
40+
}
41+
42+
public class ElfBlacksmith implements Blacksmith {
43+
public Weapon manufactureWeapon(WeaponType weaponType) {
44+
return new ElfWeapon(weaponType);
45+
}
46+
}
47+
48+
public class OrcBlacksmith implements Blacksmith {
49+
public Weapon manufactureWeapon(WeaponType weaponType) {
50+
return new OrcWeapon(weaponType);
51+
}
52+
}
53+
```
54+
55+
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
56+
```
57+
Blacksmith blacksmith = new ElfBlacksmith();
58+
blacksmith.manufactureWeapon(WeaponType.SPEAR);
59+
blacksmith.manufactureWeapon(WeaponType.AXE);
60+
// Elvish weapons are created
61+
```
2362

2463
## Applicability
2564
Use the Factory Method pattern when

factory-method/etc/factory-method.png

-21.8 KB
Binary file not shown.

factory-method/etc/factory-method.ucls

Lines changed: 0 additions & 117 deletions
This file was deleted.

factory-method/etc/factory-method.urm.puml

Lines changed: 0 additions & 54 deletions
This file was deleted.
-46.5 KB
Binary file not shown.

pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,7 @@
461461
<!-- skip for parent project -->
462462
<param>java-design-patterns</param>
463463
<param>singleton</param>
464+
<param>factory-method</param>
464465
</skipForProjects>
465466
</configuration>
466467
</plugin>

0 commit comments

Comments
 (0)