2016-01-17 13:45:33 +01:00
|
|
|
---
|
|
|
|
layout: pattern
|
|
|
|
title: Factory Kit
|
|
|
|
folder: factory-kit
|
|
|
|
permalink: /patterns/factory-kit/
|
|
|
|
categories: Creational
|
2021-05-19 10:49:05 -06:00
|
|
|
language: en
|
2016-01-17 13:45:33 +01:00
|
|
|
tags:
|
2019-12-13 21:09:28 +02:00
|
|
|
- Extensibility
|
2016-01-17 13:45:33 +01:00
|
|
|
---
|
|
|
|
|
|
|
|
## Intent
|
2022-01-08 14:29:30 +02:00
|
|
|
|
2016-02-21 12:54:40 +01:00
|
|
|
Define a factory of immutable content with separated builder and factory interfaces.
|
2016-01-17 13:45:33 +01:00
|
|
|
|
2022-01-08 14:29:30 +02:00
|
|
|
## Explanation
|
|
|
|
|
|
|
|
Real-world example
|
|
|
|
|
|
|
|
> 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.
|
|
|
|
|
|
|
|
In plain words
|
|
|
|
|
|
|
|
> Factory kit is a configurable object builder.
|
|
|
|
|
|
|
|
**Programmatic Example**
|
|
|
|
|
|
|
|
Let's first define the simple `Weapon` hierarchy.
|
|
|
|
|
|
|
|
```java
|
|
|
|
public interface Weapon {
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum WeaponType {
|
|
|
|
SWORD,
|
|
|
|
AXE,
|
|
|
|
BOW,
|
|
|
|
SPEAR
|
|
|
|
}
|
|
|
|
|
|
|
|
public class Sword implements Weapon {
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
|
|
|
return "Sword";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Axe, Bow, and Spear are defined similarly
|
|
|
|
```
|
|
|
|
|
|
|
|
Next, we define a functional interface that allows adding a builder with a name to the factory.
|
|
|
|
|
|
|
|
```java
|
|
|
|
public interface Builder {
|
|
|
|
void add(WeaponType name, Supplier<Weapon> supplier);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The meat of the example is the `WeaponFactory` interface that effectively implements the factory
|
|
|
|
kit pattern. The method `#factory` is used to configure the factory with the classes it needs to
|
|
|
|
be able to construct. The method `#create` is then used to create object instances.
|
|
|
|
|
|
|
|
```java
|
|
|
|
public interface WeaponFactory {
|
|
|
|
|
|
|
|
static WeaponFactory factory(Consumer<Builder> consumer) {
|
|
|
|
var map = new HashMap<WeaponType, Supplier<Weapon>>();
|
|
|
|
consumer.accept(map::put);
|
|
|
|
return name -> map.get(name).get();
|
|
|
|
}
|
|
|
|
|
|
|
|
Weapon create(WeaponType name);
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
Now, we can show how `WeaponFactory` can be used.
|
|
|
|
|
|
|
|
```java
|
|
|
|
var factory = WeaponFactory.factory(builder -> {
|
|
|
|
builder.add(WeaponType.SWORD, Sword::new);
|
|
|
|
builder.add(WeaponType.AXE, Axe::new);
|
|
|
|
builder.add(WeaponType.SPEAR, Spear::new);
|
|
|
|
builder.add(WeaponType.BOW, Bow::new);
|
|
|
|
});
|
|
|
|
var list = new ArrayList<Weapon>();
|
|
|
|
list.add(factory.create(WeaponType.AXE));
|
|
|
|
list.add(factory.create(WeaponType.SPEAR));
|
|
|
|
list.add(factory.create(WeaponType.SWORD));
|
|
|
|
list.add(factory.create(WeaponType.BOW));
|
|
|
|
list.stream().forEach(weapon -> LOGGER.info("{}", weapon.toString()));
|
|
|
|
```
|
|
|
|
|
|
|
|
Here is the console output when the example is run.
|
|
|
|
|
|
|
|
```
|
|
|
|
21:15:49.709 [main] INFO com.iluwatar.factorykit.App - Axe
|
|
|
|
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Spear
|
|
|
|
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Sword
|
|
|
|
21:15:49.713 [main] INFO com.iluwatar.factorykit.App - Bow
|
|
|
|
```
|
|
|
|
|
2019-12-07 20:01:13 +02:00
|
|
|
## Class diagram
|
2022-01-08 14:29:30 +02:00
|
|
|
|
2016-02-15 20:37:16 +01:00
|
|
|

|
2016-01-17 13:45:33 +01:00
|
|
|
|
|
|
|
## Applicability
|
2022-01-08 14:29:30 +02:00
|
|
|
|
2016-01-17 13:45:33 +01:00
|
|
|
Use the Factory Kit pattern when
|
|
|
|
|
2022-01-08 14:29:30 +02:00
|
|
|
* The factory class can't anticipate the types of objects it must create
|
|
|
|
* A new instance of a custom builder is needed instead of a global one
|
|
|
|
* The types of objects that the factory can build need to be defined outside the class
|
|
|
|
* The builder and creator interfaces need to be separated
|
|
|
|
|
|
|
|
## Related patterns
|
|
|
|
|
|
|
|
* [Builder](https://java-design-patterns.com/patterns/builder/)
|
|
|
|
* [Factory](https://java-design-patterns.com/patterns/factory/)
|
2016-01-17 13:45:33 +01:00
|
|
|
|
|
|
|
## Credits
|
|
|
|
|
2022-01-08 14:29:30 +02:00
|
|
|
* [Design Pattern Reloaded by Remi Forax](https://www.youtube.com/watch?v=-k2X7guaArU)
|