You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: factory-method/README.md
+29-12Lines changed: 29 additions & 12 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -10,29 +10,37 @@ tags:
10
10
---
11
11
12
12
## Also known as
13
+
13
14
Virtual Constructor
14
15
15
16
## Intent
16
-
Define an interface for creating an object, but let subclasses
17
-
decide which class to instantiate. Factory Method lets a class defer
18
-
instantiation to subclasses.
17
+
18
+
Define an interface for creating an object, but let subclasses decide which class to instantiate.
19
+
Factory Method lets a class defer instantiation to subclasses.
19
20
20
21
## Explanation
22
+
21
23
Real world example
22
24
23
-
> 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.
> Depending on the customer at hand the right type of blacksmith is summoned.
24
27
25
28
In plain words
26
29
27
30
> It provides a way to delegate the instantiation logic to child classes.
28
31
29
32
Wikipedia says
30
33
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.
34
+
> In class-based programming, the factory method pattern is a creational pattern that uses factory
35
+
> methods to deal with the problem of creating objects without having to specify the exact class of
36
+
> the object that will be created. This is done by creating objects by calling a factory method
37
+
> — either specified in an interface and implemented by child classes, or implemented in a base
38
+
> class and optionally overridden by derived classes—rather than by calling a constructor.
32
39
33
40
**Programmatic Example**
34
41
35
-
Taking our blacksmith example above. First of all we have a blacksmith interface and some implementations for it
42
+
Taking our blacksmith example above. First of all we have a `Blacksmith` interface and some
43
+
implementations for it:
36
44
37
45
```java
38
46
publicinterfaceBlacksmith {
@@ -52,24 +60,33 @@ public class OrcBlacksmith implements Blacksmith {
52
60
}
53
61
```
54
62
55
-
Now as the customers come the correct type of blacksmith is summoned and requested weapons are manufactured
63
+
When the customers come, the correct type of blacksmith is summoned and requested weapons are
64
+
manufactured:
56
65
57
66
```java
58
67
var blacksmith =newElfBlacksmith();
59
68
blacksmith.manufactureWeapon(WeaponType.SPEAR);
60
69
blacksmith.manufactureWeapon(WeaponType.AXE);
61
-
// Elvish weapons are created
70
+
```
71
+
72
+
Program output:
73
+
```java
74
+
// Elven spear
75
+
// Elven axe
62
76
```
63
77
64
78
## Class diagram
79
+
65
80

66
81
67
82
## Applicability
68
-
Use the Factory Method pattern when
69
83
70
-
* a class can't anticipate the class of objects it must create
71
-
* a class wants its subclasses to specify the objects it creates
72
-
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
84
+
Use the Factory Method pattern when:
85
+
86
+
* Class cannot anticipate the class of objects it must create.
87
+
* Class wants its subclasses to specify the objects it creates.
88
+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
89
+
knowledge of which helper subclass is the delegate.
0 commit comments