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/README.md
+19-15Lines changed: 19 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,11 @@ title: Factory
3
3
category: Creational
4
4
language: en
5
5
tag:
6
+
- Abstraction
7
+
- Encapsulation
6
8
- Gang of Four
7
9
- Instantiation
10
+
- Polymorphism
8
11
---
9
12
10
13
## Intent
@@ -15,14 +18,16 @@ The Factory design pattern is intended to define an interface for creating an ob
15
18
16
19
Real-world example
17
20
18
-
> Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both gold and copper coins and switching between them must be possible without modifying the existing source code. The factory pattern makes it possible by providing a static construction method which can be called with relevant parameters.
21
+
> Imagine a scenario in a bakery where different types of cakes are made. The bakery has a "CakeFactory" where customers can order cakes. The CakeFactory can produce various types of cakes such as chocolate cake, vanilla cake, and strawberry cake. Instead of the bakery staff manually selecting ingredients and following specific recipes for each type of cake, they use the CakeFactory to handle the process. The customer simply requests a cake type, and the CakeFactory determines the appropriate ingredients and recipe to use, then creates the specific type of cake. This setup allows the bakery to easily add new cake types without modifying the core cake-making process, promoting flexibility and scalability.
19
22
20
23
Wikipedia says
21
24
22
25
> Factory is an object for creating other objects – formally a factory is a function or method that returns objects of a varying prototype or class.
23
26
24
27
**Programmatic Example**
25
28
29
+
Imagine an alchemist who is about to manufacture coins. The alchemist must be able to create both gold and copper coins and switching between them must be possible without modifying the existing source code. The factory pattern makes it possible by providing a static construction method which can be called with relevant parameters.
30
+
26
31
We have an interface `Coin` and two implementations `GoldCoin` and `CopperCoin`.
27
32
28
33
```java
@@ -76,27 +81,25 @@ public class CoinFactory {
76
81
}
77
82
```
78
83
79
-
Now on the client code we can create different types of coins using the factory class.
84
+
Now, in the client code, we can generate various types of coins using the factory class.
80
85
81
86
```java
82
-
LOGGER.info("The alchemist begins his work.");
83
-
var coin1 =CoinFactory.getCoin(CoinType.COPPER);
84
-
var coin2 =CoinFactory.getCoin(CoinType.GOLD);
85
-
LOGGER.info(coin1.getDescription());
86
-
LOGGER.info(coin2.getDescription());
87
+
publicstaticvoid main(String[] args) {
88
+
LOGGER.info("The alchemist begins his work.");
89
+
var coin1 =CoinFactory.getCoin(CoinType.COPPER);
90
+
var coin2 =CoinFactory.getCoin(CoinType.GOLD);
91
+
LOGGER.info(coin1.getDescription());
92
+
LOGGER.info(coin2.getDescription());
93
+
}
87
94
```
88
95
89
96
Program output:
90
97
91
-
```java
92
-
The alchemist begins his work.
93
-
This is a copper coin.
94
-
This is a gold coin.
95
98
```
96
-
97
-
## ClassDiagram
98
-
99
-

99
+
06:19:53.530 [main] INFOcom.iluwatar.factory.App--The alchemist begins his work.
100
+
06:19:53.533 [main] INFOcom.iluwatar.factory.App--This is a copper coin.
101
+
06:19:53.533 [main] INFOcom.iluwatar.factory.App--This is a gold coin.
102
+
```
100
103
101
104
## Applicability
102
105
@@ -137,4 +140,5 @@ Trade-offs:
137
140
## Credits
138
141
139
142
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0Rk5y)
143
+
* [Effective Java](https://amzn.to/4cGk2Jz)
140
144
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)
0 commit comments