issue #333 diagrams and index added
This commit is contained in:
parent
10bbf988ea
commit
022ab28e20
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>
|
@ -10,21 +10,18 @@ tags:
|
||||
- Functional
|
||||
---
|
||||
|
||||
## Also known as
|
||||
Virtual Constructor
|
||||
|
||||
## Intent
|
||||
Define 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 custom builder instead of global one
|
||||
* a class wants its subclasses to specify the objects it creates
|
||||
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate
|
||||
* 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
|
||||
|
||||
|
@ -2,14 +2,14 @@ package com.iluwatar.factorykit;
|
||||
|
||||
public class App {
|
||||
|
||||
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);
|
||||
}
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Axe implements Weapon {
|
||||
@Override public String toString() {
|
||||
return "Axe{}";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Axe";
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Created by crossy on 2016-01-16.
|
||||
*/
|
||||
public class Bow implements Weapon {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Bow";
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,9 @@ 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);
|
||||
void add(WeaponType name, Supplier<Weapon> supplier);
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Spear implements Weapon {
|
||||
@Override public String toString() {
|
||||
return "Spear{}";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Spear";
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,8 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
public class Sword implements Weapon {
|
||||
@Override public String toString() {
|
||||
return "Sword{}";
|
||||
}
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Sword";
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Interface representing weapon
|
||||
*/
|
||||
public interface Weapon {
|
||||
}
|
||||
|
@ -4,13 +4,18 @@ import java.util.HashMap;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Functional interface that represents factory kit. Instance created locally gives an opportunity to strictly define
|
||||
* which objects types the instance of a factory would be able to create. Factory is just a placeholder for builders with
|
||||
* create method to initialize new objects.
|
||||
*/
|
||||
public interface WeaponFactory {
|
||||
|
||||
Weapon create(WeaponType name);
|
||||
Weapon create(WeaponType name);
|
||||
|
||||
static WeaponFactory factory(Consumer<Builder> consumer) {
|
||||
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
|
||||
consumer.accept(map::put);
|
||||
return name -> map.get(name).get();
|
||||
}
|
||||
static WeaponFactory factory(Consumer<Builder> consumer) {
|
||||
HashMap<WeaponType, Supplier<Weapon>> map = new HashMap<>();
|
||||
consumer.accept(map::put);
|
||||
return name -> map.get(name).get();
|
||||
}
|
||||
}
|
||||
|
@ -1,8 +1,5 @@
|
||||
package com.iluwatar.factorykit;
|
||||
|
||||
/**
|
||||
* Created by crossy on 2016-01-16.
|
||||
*/
|
||||
public enum WeaponType {
|
||||
SWORD, AXE, BOW, SPEAR
|
||||
SWORD, AXE, BOW, SPEAR
|
||||
}
|
||||
|
@ -5,9 +5,10 @@ import org.junit.Test;
|
||||
|
||||
public class AppTest {
|
||||
|
||||
@Test public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
@Test
|
||||
public void test() {
|
||||
String[] args = {};
|
||||
App.main(args);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -6,53 +6,54 @@ import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Created by crossy on 2016-01-16.
|
||||
*/
|
||||
public class FactoryKitTest {
|
||||
|
||||
private WeaponFactory factory;
|
||||
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);
|
||||
});
|
||||
}
|
||||
@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 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 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);
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
/**
|
||||
* 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));
|
||||
}
|
||||
}
|
||||
|
@ -19,57 +19,61 @@ import static org.junit.Assert.assertTrue;
|
||||
*/
|
||||
public class FactoryMethodTest {
|
||||
|
||||
/**
|
||||
* Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
|
||||
* of {@link OrcWeapon}.
|
||||
*/
|
||||
@Test public void testOrcBlacksmithWithSpear() {
|
||||
Blacksmith blacksmith = new OrcBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class);
|
||||
}
|
||||
/**
|
||||
* Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
|
||||
* of {@link OrcWeapon}.
|
||||
*/
|
||||
@Test
|
||||
public void testOrcBlacksmithWithSpear() {
|
||||
Blacksmith blacksmith = new OrcBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance
|
||||
* of {@link OrcWeapon}.
|
||||
*/
|
||||
@Test public void testOrcBlacksmithWithAxe() {
|
||||
Blacksmith blacksmith = new OrcBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class);
|
||||
}
|
||||
/**
|
||||
* Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance
|
||||
* of {@link OrcWeapon}.
|
||||
*/
|
||||
@Test
|
||||
public void testOrcBlacksmithWithAxe() {
|
||||
Blacksmith blacksmith = new OrcBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
|
||||
verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an
|
||||
* instance of {@link ElfWeapon}.
|
||||
*/
|
||||
@Test public void testElfBlacksmithWithShortSword() {
|
||||
Blacksmith blacksmith = new ElfBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
||||
verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class);
|
||||
}
|
||||
/**
|
||||
* Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an
|
||||
* instance of {@link ElfWeapon}.
|
||||
*/
|
||||
@Test
|
||||
public void testElfBlacksmithWithShortSword() {
|
||||
Blacksmith blacksmith = new ElfBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
|
||||
verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
|
||||
* of {@link ElfWeapon}.
|
||||
*/
|
||||
@Test public void testElfBlacksmithWithSpear() {
|
||||
Blacksmith blacksmith = new ElfBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class);
|
||||
}
|
||||
/**
|
||||
* Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
|
||||
* of {@link ElfWeapon}.
|
||||
*/
|
||||
@Test
|
||||
public void testElfBlacksmithWithSpear() {
|
||||
Blacksmith blacksmith = new ElfBlacksmith();
|
||||
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
|
||||
verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class);
|
||||
}
|
||||
|
||||
/**
|
||||
* This method asserts that the weapon object that is passed is an instance of the clazz and the
|
||||
* weapon is of type expectedWeaponType.
|
||||
*
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param expectedWeaponType expected WeaponType of the weapon
|
||||
* @param clazz expected class of the weapon
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) {
|
||||
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
|
||||
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
|
||||
weapon.getWeaponType());
|
||||
}
|
||||
/**
|
||||
* This method asserts that the weapon object that is passed is an instance of the clazz and the
|
||||
* weapon is of type expectedWeaponType.
|
||||
*
|
||||
* @param weapon weapon object which is to be verified
|
||||
* @param expectedWeaponType expected WeaponType of the weapon
|
||||
* @param clazz expected class of the weapon
|
||||
*/
|
||||
private void verifyWeapon(Weapon weapon, WeaponType expectedWeaponType, Class clazz) {
|
||||
assertTrue("Weapon must be an object of: " + clazz.getName(), clazz.isInstance(weapon));
|
||||
assertEquals("Weapon must be of weaponType: " + clazz.getName(), expectedWeaponType,
|
||||
weapon.getWeaponType());
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user