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
+33-21Lines changed: 33 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ title: Factory Method
3
3
category: Creational
4
4
language: en
5
5
tag:
6
-
- Extensibility
7
-
- Gang of Four
6
+
- Encapsulation
7
+
- Gang of Four
8
+
- Instantiation
9
+
- Object composition
10
+
- Polymorphism
8
11
---
9
12
10
13
## Also known as
@@ -13,32 +16,25 @@ Virtual Constructor
13
16
14
17
## Intent
15
18
16
-
Define an interface for creating an object, but let subclasses decide which class to instantiate.
17
-
Factory Method lets a class defer instantiation to subclasses.
19
+
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.
> Depending on the customer at hand the right type of blacksmith is summoned.
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.
25
26
26
27
In plain words
27
28
28
29
> It provides a way to delegate the instantiation logic to child classes.
29
30
30
31
Wikipedia says
31
32
32
-
> In class-based programming, the factory method pattern is a creational pattern that uses factory
33
-
> methods to deal with the problem of creating objects without having to specify the exact class of
34
-
> the object that will be created. This is done by creating objects by calling a factory method
35
-
> — either specified in an interface and implemented by child classes, or implemented in a base
36
-
> class and optionally overridden by derived classes—rather than by calling a constructor.
33
+
> 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.
37
34
38
-
**Programmatic Example**
35
+
**Programmatic Example**
39
36
40
-
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some
41
-
implementations for it:
37
+
Taking our blacksmith example above. First of all, we have a `Blacksmith` interface and some implementations for it:
42
38
43
39
```java
44
40
publicinterfaceBlacksmith {
@@ -58,8 +54,7 @@ public class OrcBlacksmith implements Blacksmith {
58
54
}
59
55
```
60
56
61
-
When the customers come, the correct type of blacksmith is summoned and requested weapons are
62
-
manufactured:
57
+
When the customers come, the correct type of blacksmith is summoned and requested weapons are manufactured:
63
58
64
59
```java
65
60
Blacksmith blacksmith =newOrcBlacksmith();
@@ -93,8 +88,7 @@ Use the Factory Method pattern when:
93
88
94
89
* Class cannot anticipate the class of objects it must create.
95
90
* Class wants its subclasses to specify the objects it creates.
96
-
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the
97
-
knowledge of which helper subclass is the delegate.
91
+
* Classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate.
98
92
99
93
## Known uses
100
94
@@ -105,9 +99,27 @@ knowledge of which helper subclass is the delegate.
* Frameworks that run application components, configured dynamically at runtime.
103
+
104
+
## Consequences
105
+
106
+
Benefits:
107
+
108
+
* Provides hooks for subclasses, creating flexibility in code.
109
+
* Connects parallel class hierarchies.
110
+
* Eliminates the need to bind application-specific classes into the code. The code only deals with the product interface; hence it can work with any user-defined concrete product classes.
111
+
112
+
Trade-offs:
113
+
114
+
* Can complicate the code by requiring the addition of new subclasses to implement the extended factory methods.
115
+
116
+
## Related Patterns
117
+
118
+
*[Abstract Factory](https://java-design-patterns.com/patterns/abstract-factory/): Factory methods are often called within Abstract Factory patterns.
119
+
*[Prototype](https://java-design-patterns.com/patterns/prototype/): A factory method that returns a new instance of a class that is a clone of a prototype class.
108
120
109
121
## Credits
110
122
111
-
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://www.amazon.com/gp/product/0201633612/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0201633612&linkCode=as2&tag=javadesignpat-20&linkId=675d49790ce11db99d90bde47f1aeb59)
112
-
*[Head First Design Patterns: A Brain-Friendly Guide](https://www.amazon.com/gp/product/0596007124/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0596007124&linkCode=as2&tag=javadesignpat-20&linkId=6b8b6eea86021af6c8e3cd3fc382cb5b)
113
-
*[Refactoring to Patterns](https://www.amazon.com/gp/product/0321213351/ref=as_li_tl?ie=UTF8&camp=1789&creative=9325&creativeASIN=0321213351&linkCode=as2&tag=javadesignpat-20&linkId=2a76fcb387234bc71b1c61150b3cc3a7)
123
+
*[Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0Rk5y)
124
+
*[Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)
125
+
*[Patterns of Enterprise Application Architecture](https://amzn.to/4b2ZxoM)
Copy file name to clipboardExpand all lines: factory/README.md
-4Lines changed: 0 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,10 +7,6 @@ tag:
7
7
- Instantiation
8
8
---
9
9
10
-
## Also known as
11
-
12
-
* Factory Method
13
-
14
10
## Intent
15
11
16
12
The Factory design pattern is intended to define an interface for creating an object, but allows subclasses to alter the type of objects that will be created. This pattern is particularly useful when the creation process involves complexity.
0 commit comments