commit
9f086ba72c
BIN
factory-kit/etc/factory-kit.png
Normal file
BIN
factory-kit/etc/factory-kit.png
Normal file
Binary file not shown.
After Width: | Height: | Size: 37 KiB |
10
factory-kit/etc/factory-kit.ucls
Normal file
10
factory-kit/etc/factory-kit.ucls
Normal file
@ -0,0 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<class-diagram version="1.1.9" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
|
||||
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
|
||||
<classifier-display autosize="true" stereotype="true" package="true" initial-value="false" signature="true"
|
||||
sort-features="false" accessors="true" visibility="true">
|
||||
<attributes public="true" package="true" protected="true" private="true" static="true"/>
|
||||
<operations public="true" package="true" protected="true" private="true" static="true"/>
|
||||
</classifier-display>
|
||||
<association-display labels="true" multiplicity="true"/>
|
||||
</class-diagram>
|
28
factory-kit/index.md
Normal file
28
factory-kit/index.md
Normal file
@ -0,0 +1,28 @@
|
||||
---
|
||||
layout: pattern
|
||||
title: Factory Kit
|
||||
folder: factory-kit
|
||||
permalink: /patterns/factory-kit/
|
||||
categories: Creational
|
||||
tags:
|
||||
- Java
|
||||
- Difficulty-Beginner
|
||||
- Functional
|
||||
---
|
||||
|
||||
## Intent
|
||||
Define a factory of immutable content with separated builder and factory interfaces.
|
||||
|
||||

|
||||
|
||||
## Applicability
|
||||
Use the Factory Kit pattern when
|
||||
|
||||
* a class can't anticipate the class of objects it must create
|
||||
* you just want a new instance of a custom builder instead of the global one
|
||||
* you explicitly want to define types of objects, that factory can build
|
||||
* you want a separated builder and creator interface
|
||||
|
||||
## Credits
|
||||
|
||||
* [Design Pattern Reloaded by Remi Forax: ](https://www.youtube.com/watch?v=-k2X7guaArU)
|
48
factory-kit/pom.xml
Normal file
48
factory-kit/pom.xml
Normal file
@ -0,0 +1,48 @@
|
||||
<?xml version="1.0"?>
|
||||
<!--
|
||||
|
||||
The MIT License
|
||||
Copyright (c) 2014 Ilkka Seppälä
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
|
||||
-->
|
||||
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>com.iluwatar</groupId>
|
||||
<artifactId>java-design-patterns</artifactId>
|
||||
<version>1.11.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
<artifactId>factory-kit</artifactId>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.mockito</groupId>
|
||||
<artifactId>mockito-core</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
32
factory-kit/src/main/java/com/iluwatar/factorykit/App.java
Normal file
32
factory-kit/src/main/java/com/iluwatar/factorykit/App.java
Normal file
@ -0,0 +1,32 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Factory-kit is a creational pattern which defines a factory of immutable content
|
||||
* with separated builder and factory interfaces to deal with the problem of
|
||||
* creating one of the objects specified directly in the factory-kit instance.
|
||||
*
|
||||
* <p>
|
||||
* In the given example {@link WeaponFactory} represents the factory-kit, that contains
|
||||
* four {@link Builder}s for creating new objects of
|
||||
* the classes implementing {@link Weapon} interface.
|
||||
* <br>Each of them can be called with {@link WeaponFactory#create(WeaponType)} method, with
|
||||
* an input representing an instance of {@link WeaponType} that needs to
|
||||
* be mapped explicitly with desired class type in the factory instance.
|
||||
*/
|
||||
public class App {
|
||||
/**
|
||||
* Program entry point.
|
||||
*
|
||||
* @param args @param args command line args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
WeaponFactory 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);
|
||||
});
|
||||
Weapon axe = factory.create(WeaponType.AXE);
|
||||
System.out.println(axe);
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Axe implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Axe";
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Bow implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bow";
|
||||
}
|
||||
}
|
@ -0,0 +1,10 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Functional interface that allows adding builder with name to the factory.
|
||||
*/
|
||||
public interface Builder {
|
||||
void add(WeaponType name, Supplier<Weapon> supplier);
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Spear implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Spear";
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Sword implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Sword";
|
||||
}
|
||||
}
|
@ -0,0 +1,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Interface representing weapon.
|
||||
*/
|
||||
public interface Weapon {
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Functional interface, an example of the factory-kit design pattern.
|
||||
* <br>Instance created locally gives an opportunity to strictly define
|
||||
* which objects types the instance of a factory will be able to create.
|
||||
* <br>Factory is a placeholder for {@link Builder}s
|
||||
* with {@link WeaponFactory#create(WeaponType)} method to initialize new objects.
|
||||
*/
|
||||
public interface WeaponFactory {
|
||||
|
||||
/**
|
||||
* Creates an instance of the given type.
|
||||
* @param name representing enum of an object type to be created.
|
||||
* @return new instance of a requested class implementing {@link Weapon} interface.
|
||||
*/
|
||||
Weapon create(WeaponType name);
|
||||
|
||||
/**
|
||||
* Creates factory - placeholder for specified {@link Builder}s.
|
||||
* @param consumer for the new builder to the factory.
|
||||
* @return factory with specified {@link Builder}s
|
||||
*/
|
||||
static WeaponFactory factory(Consumer<Builder> consumer) {
|
||||
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
|
||||
consumer.accept(map::put);
|
||||
return name -> map.get(name).get();
|
||||
}
|
||||
}
|
@ -0,0 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Enumerates {@link Weapon} types
|
||||
*/
|
||||
public enum WeaponType {
|
||||
SWORD, AXE, BOW, SPEAR
|
||||
}
|
@ -0,0 +1,14 @@
|
||||
package com.iluwatar.factorykit.app;
|
||||
|
||||
import com.iluwatar.factorykit.App;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,59 @@
|
||||
package com.iluwatar.factorykit.factorykit;
|
||||
|
||||
import com.iluwatar.factorykit.*;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
public class FactoryKitTest {
|
||||
|
||||
private WeaponFactory factory;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
factory = WeaponFactory.factory(builder -> {
|
||||
builder.add(WeaponType.SPEAR, Spear::new);
|
||||
builder.add(WeaponType.AXE, Axe::new);
|
||||
builder.add(WeaponType.SWORD, Sword::new);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing {@link WeaponFactory} to produce a SPEAR asserting that the Weapon is an instance of {@link Spear}
|
||||
*/
|
||||
@Test
|
||||
public void testSpearWeapon() {
|
||||
Weapon weapon = factory.create(WeaponType.SPEAR);
|
||||
verifyWeapon(weapon, Spear.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing {@link WeaponFactory} to produce a AXE asserting that the Weapon is an instance of {@link Axe}
|
||||
*/
|
||||
@Test
|
||||
public void testAxeWeapon() {
|
||||
Weapon weapon = factory.create(WeaponType.AXE);
|
||||
verifyWeapon(weapon, Axe.class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of {@link Sword}
|
||||
*/
|
||||
@Test
|
||||
public void testWeapon() {
|
||||
Weapon weapon = factory.create(WeaponType.SWORD);
|
||||
verifyWeapon(weapon, Sword.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method asserts that the weapon object that is passed is an instance of the clazz
|
||||
*
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param clazz expected class of the weapon
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, Class clazz) {
|
||||
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user