Skip to content

Commit 10bbf98

Browse files
committed
issue iluwatar#333 factory kit pattern introduced
1 parent bd82387 commit 10bbf98

15 files changed

Lines changed: 255 additions & 57 deletions

File tree

factory-kit/index.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
layout: pattern
3+
title: Factory Kit
4+
folder: factory-kit
5+
permalink: /patterns/factory-kit/
6+
categories: Creational
7+
tags:
8+
- Java
9+
- Difficulty-Beginner
10+
- Functional
11+
---
12+
13+
## Also known as
14+
Virtual Constructor
15+
16+
## Intent
17+
Define factory of immutable content with separated builder and factory interfaces.
18+
19+
![alt text](./etc/factory-kit_1.png "Factory Kit")
20+
21+
## Applicability
22+
Use the Factory Kit pattern when
23+
24+
* a class can't anticipate the class of objects it must create
25+
* you just want a new instance of custom builder instead of global one
26+
* a class wants its subclasses to specify the objects it creates
27+
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
28+
29+
## Credits
30+
31+
* [Design Pattern Reloaded by Remi Forax: ](https://www.youtube.com/watch?v=-k2X7guaArU)

factory-kit/pom.xml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>java-design-patterns</artifactId>
7+
<groupId>com.iluwatar</groupId>
8+
<version>1.10.0-SNAPSHOT</version>
9+
</parent>
10+
<modelVersion>4.0.0</modelVersion>
11+
<dependencies>
12+
<dependency>
13+
<groupId>junit</groupId>
14+
<artifactId>junit</artifactId>
15+
<scope>test</scope>
16+
</dependency>
17+
</dependencies>
18+
<artifactId>factory-kit</artifactId>
19+
20+
21+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.iluwatar.factorykit;
2+
3+
public class App {
4+
5+
public static void main(String[] args) {
6+
WeaponFactory factory = WeaponFactory.factory(builder -> {
7+
builder.add(WeaponType.SWORD, Sword::new);
8+
builder.add(WeaponType.AXE, Axe::new);
9+
builder.add(WeaponType.SPEAR, Spear::new);
10+
builder.add(WeaponType.BOW, Bow::new);
11+
});
12+
Weapon axe = factory.create(WeaponType.AXE);
13+
System.out.println(axe);
14+
}
15+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.factorykit;
2+
3+
public class Axe implements Weapon {
4+
@Override public String toString() {
5+
return "Axe{}";
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.factorykit;
2+
3+
/**
4+
* Created by crossy on 2016-01-16.
5+
*/
6+
public class Bow implements Weapon {
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.factorykit;
2+
3+
import java.util.function.Supplier;
4+
5+
public interface Builder {
6+
void add(WeaponType name, Supplier<Weapon> supplier);
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.factorykit;
2+
3+
public class Spear implements Weapon {
4+
@Override public String toString() {
5+
return "Spear{}";
6+
}
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package com.iluwatar.factorykit;
2+
3+
public class Sword implements Weapon {
4+
@Override public String toString() {
5+
return "Sword{}";
6+
}
7+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package com.iluwatar.factorykit;
2+
3+
public interface Weapon {
4+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.iluwatar.factorykit;
2+
3+
import java.util.HashMap;
4+
import java.util.function.Consumer;
5+
import java.util.function.Supplier;
6+
7+
public interface WeaponFactory {
8+
9+
Weapon create(WeaponType name);
10+
11+
static WeaponFactory factory(Consumer<Builder> consumer) {
12+
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
13+
consumer.accept(map::put);
14+
return name -> map.get(name).get();
15+
}
16+
}

0 commit comments

Comments
 (0)