Skip to content

Commit 926e0b3

Browse files
committed
docs: update factory
1 parent 325b580 commit 926e0b3

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

factory/README.md

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ title: Factory
33
category: Creational
44
language: en
55
tag:
6+
- Abstraction
7+
- Encapsulation
68
- Gang of Four
79
- Instantiation
10+
- Polymorphism
811
---
912

1013
## Intent
@@ -15,14 +18,16 @@ The Factory design pattern is intended to define an interface for creating an ob
1518

1619
Real-world example
1720

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.
1922
2023
Wikipedia says
2124

2225
> 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.
2326
2427
**Programmatic Example**
2528

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+
2631
We have an interface `Coin` and two implementations `GoldCoin` and `CopperCoin`.
2732

2833
```java
@@ -76,27 +81,25 @@ public class CoinFactory {
7681
}
7782
```
7883

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.
8085

8186
```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+
public static void 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+
}
8794
```
8895

8996
Program output:
9097

91-
```java
92-
The alchemist begins his work.
93-
This is a copper coin.
94-
This is a gold coin.
9598
```
96-
97-
## Class Diagram
98-
99-
![alt text](./etc/factory.urm.png "Factory pattern class diagram")
99+
06:19:53.530 [main] INFO com.iluwatar.factory.App -- The alchemist begins his work.
100+
06:19:53.533 [main] INFO com.iluwatar.factory.App -- This is a copper coin.
101+
06:19:53.533 [main] INFO com.iluwatar.factory.App -- This is a gold coin.
102+
```
100103

101104
## Applicability
102105

@@ -137,4 +140,5 @@ Trade-offs:
137140
## Credits
138141

139142
* [Design Patterns: Elements of Reusable Object-Oriented Software](https://amzn.to/3w0Rk5y)
143+
* [Effective Java](https://amzn.to/4cGk2Jz)
140144
* [Head First Design Patterns: Building Extensible and Maintainable Object-Oriented Software](https://amzn.to/3UpTLrG)

0 commit comments

Comments
 (0)