Skip to content

Commit a4ded4b

Browse files
committed
docs: update factory kit
1 parent 926e0b3 commit a4ded4b

File tree

1 file changed

+27
-26
lines changed

1 file changed

+27
-26
lines changed

factory-kit/README.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ title: Factory Kit
33
category: Creational
44
language: en
55
tag:
6+
- Abstraction
67
- Decoupling
78
- Encapsulation
89
- Generic
@@ -23,14 +24,16 @@ Define a factory of immutable content with separated builder and factory interfa
2324

2425
Real-world example
2526

26-
> Imagine a magical weapon factory that can create any type of weapon wished for. When the factory is unboxed, the master recites the weapon types needed to prepare it. After that, any of those weapon types can be summoned in an instant.
27+
> An analogous real-world example of the Factory Kit pattern is a restaurant kitchen where different types of dishes are prepared. Imagine the kitchen has a central station with various ingredients and recipes registered for different dishes. When an order comes in, the chef consults this central station to gather the necessary ingredients and follow the registered recipe to prepare the dish. This setup allows the kitchen to efficiently manage and switch between different dish preparations without the need for each chef to know the specifics of every recipe, promoting flexibility and consistency in the cooking process.
2728
2829
In plain words
2930

3031
> Factory kit is a configurable object builder, a factory to create factories.
3132
3233
**Programmatic Example**
3334

35+
Imagine a magical weapon factory capable of creating any desired weapon. Upon activation, the master recites the names of the weapon types needed to configure it. Once set up, any of these weapon types can be summoned instantly.
36+
3437
Let's first define the simple `Weapon` hierarchy.
3538

3639
```java
@@ -80,33 +83,31 @@ public interface WeaponFactory {
8083
Now, we can show how `WeaponFactory` can be used.
8184

8285
```java
83-
var factory = WeaponFactory.factory( builder-> {
84-
builder.add(WeaponType.SWORD,Sword::new);
85-
builder.add(WeaponType.AXE,Axe::new);
86-
builder.add(WeaponType.SPEAR,Spear::new);
87-
builder.add(WeaponType.BOW,Bow::new);
88-
});
89-
var list = new ArrayList<Weapon>();
90-
list.add(factory.create(WeaponType.AXE));
91-
list.add(factory.create(WeaponType.SPEAR));
92-
list.add(factory.create(WeaponType.SWORD));
93-
list.add(factory.create(WeaponType.BOW));
94-
list.stream().forEach(weapon->LOGGER.info("{}",weapon.toString()));
86+
public static void main(String[] args) {
87+
var factory = WeaponFactory.factory(builder -> {
88+
builder.add(WeaponType.SWORD, Sword::new);
89+
builder.add(WeaponType.AXE, Axe::new);
90+
builder.add(WeaponType.SPEAR, Spear::new);
91+
builder.add(WeaponType.BOW, Bow::new);
92+
});
93+
var list = new ArrayList<Weapon>();
94+
list.add(factory.create(WeaponType.AXE));
95+
list.add(factory.create(WeaponType.SPEAR));
96+
list.add(factory.create(WeaponType.SWORD));
97+
list.add(factory.create(WeaponType.BOW));
98+
list.forEach(weapon -> LOGGER.info("{}", weapon.toString()));
99+
}
95100
```
96101

97102
Here is the console output when the example is run.
98103

99104
```
100-
21:15:49.709 [main] INFO com.iluwatar.factorykit.App - Axe
101-
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Spear
102-
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Sword
103-
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Bow
105+
06:32:23.026 [main] INFO com.iluwatar.factorykit.App -- Axe
106+
06:32:23.029 [main] INFO com.iluwatar.factorykit.App -- Spear
107+
06:32:23.029 [main] INFO com.iluwatar.factorykit.App -- Sword
108+
06:32:23.029 [main] INFO com.iluwatar.factorykit.App -- Bow
104109
```
105110

106-
## Class diagram
107-
108-
![alt text](./etc/factory-kit.png "Factory Kit")
109-
110111
## Applicability
111112

112113
Use the Factory Kit pattern when
@@ -117,6 +118,10 @@ Use the Factory Kit pattern when
117118
* The builder and creator interfaces need to be separated
118119
* Game developments and other applications that have user customisation
119120

121+
## Tutorials
122+
123+
* [Factory Kit Pattern (Diego Pacheco)](https://diego-pacheco.medium.com/factory-kit-pattern-66d5ccb0c405)
124+
120125
## Known Uses
121126

122127
* In Java libraries such as the Java Development Kit (JDK) where different rendering engines might be instantiated based on the runtime environment.
@@ -140,10 +145,6 @@ Trade-offs:
140145
* [Builder](https://java-design-patterns.com/patterns/builder/): Can be used to construct complex objects step-by-step using a similar approach.
141146
* [Prototype](https://java-design-patterns.com/patterns/prototype/): Objects that are created by cloning a prototypical instance often use a factory to manage it.
142147

143-
## Tutorials
144-
145-
* [Factory kit implementation tutorial](https://diego-pacheco.medium.com/factory-kit-pattern-66d5ccb0c405)
146-
147148
## Credits
148149

149-
* [Design Pattern Reloaded by Remi Forax](https://www.youtube.com/watch?v=-k2X7guaArU)
150+
* [Design Pattern Reloaded (Remi Forax)](https://www.youtube.com/watch?v=-k2X7guaArU)

0 commit comments

Comments
 (0)