Factory Kit Pattern
- is a creational pattern used to define a factory of immutable content with the separated builder and factory method interfaces
Factory Kit Pattern Example
Define factory kit in compile time, then use it in runtime use it to create a custom factory method object, then use it to create number of objects
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
public class Main {
// define factory kit
public static <K, T> Function<K, T> factoryKit(Consumer<BiConsumer<K, T>> consumer, Function<K, T> ifAbsent) {
Map<K, T> map = new HashMap<>();
consumer.accept(map::put);
return key -> map.computeIfAbsent(key, ifAbsent);
}
public static void main(String[] args) {
// use factory kit to create custom factory method object
Function<String, Supplier<Number>> factory = factoryKit(builder -> {
builder.accept("Integer", () -> Integer.parseInt("12345678"));
builder.accept("Double", () -> Double.parseDouble("12345678.1234"));
}, name -> { throw new IllegalArgumentException("unkown number " + name); });
// use custom factory method object to create number objects
Number n1 = factory.apply("Integer").get();
Number n2 = factory.apply("Integer").get();
System.out.println(n1 == n2); // false - different objects
Number n3 = factory.apply("Double").get();
Number n4 = factory.apply("Double").get();
System.out.println(n3 == n4); // false - different objects
}
}