issue #333 diagrams and index added

This commit is contained in:
Crossy147 2016-02-15 20:37:16 +01:00
parent 10bbf988ea
commit 022ab28e20
15 changed files with 159 additions and 134 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View 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>

View File

@ -10,21 +10,18 @@ tags:
- Functional - Functional
--- ---
## Also known as
Virtual Constructor
## Intent ## Intent
Define factory of immutable content with separated builder and factory interfaces. Define factory of immutable content with separated builder and factory interfaces.
![alt text](./etc/factory-kit_1.png "Factory Kit") ![alt text](./etc/factory-kit.png "Factory Kit")
## Applicability ## Applicability
Use the Factory Kit pattern when Use the Factory Kit pattern when
* a class can't anticipate the class of objects it must create * 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 * you just want a new instance of a custom builder instead of the global one
* a class wants its subclasses to specify the objects it creates * you explicitly want to define types of objects, that factory can build
* classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate * you want a separated builder and creator interface
## Credits ## Credits

View File

@ -1,7 +1,8 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
public class Axe implements Weapon { public class Axe implements Weapon {
@Override public String toString() { @Override
return "Axe{}"; public String toString() {
return "Axe";
} }
} }

View File

@ -1,7 +1,8 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
/**
* Created by crossy on 2016-01-16.
*/
public class Bow implements Weapon { public class Bow implements Weapon {
@Override
public String toString() {
return "Bow";
}
} }

View File

@ -2,6 +2,9 @@ package com.iluwatar.factorykit;
import java.util.function.Supplier; import java.util.function.Supplier;
/**
* Functional interface that allows adding builder with name to the factory
*/
public interface Builder { public interface Builder {
void add(WeaponType name, Supplier<Weapon> supplier); void add(WeaponType name, Supplier<Weapon> supplier);
} }

View File

@ -1,7 +1,8 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
public class Spear implements Weapon { public class Spear implements Weapon {
@Override public String toString() { @Override
return "Spear{}"; public String toString() {
return "Spear";
} }
} }

View File

@ -1,7 +1,8 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
public class Sword implements Weapon { public class Sword implements Weapon {
@Override public String toString() { @Override
return "Sword{}"; public String toString() {
return "Sword";
} }
} }

View File

@ -1,4 +1,7 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
/**
* Interface representing weapon
*/
public interface Weapon { public interface Weapon {
} }

View File

@ -4,6 +4,11 @@ import java.util.HashMap;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Supplier; 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 { public interface WeaponFactory {
Weapon create(WeaponType name); Weapon create(WeaponType name);

View File

@ -1,8 +1,5 @@
package com.iluwatar.factorykit; package com.iluwatar.factorykit;
/**
* Created by crossy on 2016-01-16.
*/
public enum WeaponType { public enum WeaponType {
SWORD, AXE, BOW, SPEAR SWORD, AXE, BOW, SPEAR
} }

View File

@ -5,7 +5,8 @@ import org.junit.Test;
public class AppTest { public class AppTest {
@Test public void test() { @Test
public void test() {
String[] args = {}; String[] args = {};
App.main(args); App.main(args);
} }

View File

@ -6,14 +6,12 @@ import org.junit.Test;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
/**
* Created by crossy on 2016-01-16.
*/
public class FactoryKitTest { public class FactoryKitTest {
private WeaponFactory factory; private WeaponFactory factory;
@Before public void init() { @Before
public void init() {
factory = WeaponFactory.factory(builder -> { factory = WeaponFactory.factory(builder -> {
builder.add(WeaponType.SPEAR, Spear::new); builder.add(WeaponType.SPEAR, Spear::new);
builder.add(WeaponType.AXE, Axe::new); builder.add(WeaponType.AXE, Axe::new);
@ -24,7 +22,8 @@ public class FactoryKitTest {
/** /**
* Testing {@link WeaponFactory} to produce a SPEAR asserting that the Weapon is an instance of {@link Spear} * Testing {@link WeaponFactory} to produce a SPEAR asserting that the Weapon is an instance of {@link Spear}
*/ */
@Test public void testSpearWeapon() { @Test
public void testSpearWeapon() {
Weapon weapon = factory.create(WeaponType.SPEAR); Weapon weapon = factory.create(WeaponType.SPEAR);
verifyWeapon(weapon, Spear.class); verifyWeapon(weapon, Spear.class);
} }
@ -32,7 +31,8 @@ public class FactoryKitTest {
/** /**
* Testing {@link WeaponFactory} to produce a AXE asserting that the Weapon is an instance of {@link Axe} * Testing {@link WeaponFactory} to produce a AXE asserting that the Weapon is an instance of {@link Axe}
*/ */
@Test public void testAxeWeapon() { @Test
public void testAxeWeapon() {
Weapon weapon = factory.create(WeaponType.AXE); Weapon weapon = factory.create(WeaponType.AXE);
verifyWeapon(weapon, Axe.class); verifyWeapon(weapon, Axe.class);
} }
@ -41,7 +41,8 @@ public class FactoryKitTest {
/** /**
* Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of {@link Sword} * Testing {@link WeaponFactory} to produce a SWORD asserting that the Weapon is an instance of {@link Sword}
*/ */
@Test public void testWeapon() { @Test
public void testWeapon() {
Weapon weapon = factory.create(WeaponType.SWORD); Weapon weapon = factory.create(WeaponType.SWORD);
verifyWeapon(weapon, Sword.class); verifyWeapon(weapon, Sword.class);
} }

View File

@ -23,7 +23,8 @@ public class FactoryMethodTest {
* Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance * Testing {@link OrcBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
* of {@link OrcWeapon}. * of {@link OrcWeapon}.
*/ */
@Test public void testOrcBlacksmithWithSpear() { @Test
public void testOrcBlacksmithWithSpear() {
Blacksmith blacksmith = new OrcBlacksmith(); Blacksmith blacksmith = new OrcBlacksmith();
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class); verifyWeapon(weapon, WeaponType.SPEAR, OrcWeapon.class);
@ -33,7 +34,8 @@ public class FactoryMethodTest {
* Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance * Testing {@link OrcBlacksmith} to produce a AXE asserting that the Weapon is an instance
* of {@link OrcWeapon}. * of {@link OrcWeapon}.
*/ */
@Test public void testOrcBlacksmithWithAxe() { @Test
public void testOrcBlacksmithWithAxe() {
Blacksmith blacksmith = new OrcBlacksmith(); Blacksmith blacksmith = new OrcBlacksmith();
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE); Weapon weapon = blacksmith.manufactureWeapon(WeaponType.AXE);
verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class); verifyWeapon(weapon, WeaponType.AXE, OrcWeapon.class);
@ -43,7 +45,8 @@ public class FactoryMethodTest {
* Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an * Testing {@link ElfBlacksmith} to produce a SHORT_SWORD asserting that the Weapon is an
* instance of {@link ElfWeapon}. * instance of {@link ElfWeapon}.
*/ */
@Test public void testElfBlacksmithWithShortSword() { @Test
public void testElfBlacksmithWithShortSword() {
Blacksmith blacksmith = new ElfBlacksmith(); Blacksmith blacksmith = new ElfBlacksmith();
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD); Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SHORT_SWORD);
verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class); verifyWeapon(weapon, WeaponType.SHORT_SWORD, ElfWeapon.class);
@ -53,7 +56,8 @@ public class FactoryMethodTest {
* Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance * Testing {@link ElfBlacksmith} to produce a SPEAR asserting that the Weapon is an instance
* of {@link ElfWeapon}. * of {@link ElfWeapon}.
*/ */
@Test public void testElfBlacksmithWithSpear() { @Test
public void testElfBlacksmithWithSpear() {
Blacksmith blacksmith = new ElfBlacksmith(); Blacksmith blacksmith = new ElfBlacksmith();
Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR); Weapon weapon = blacksmith.manufactureWeapon(WeaponType.SPEAR);
verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class); verifyWeapon(weapon, WeaponType.SPEAR, ElfWeapon.class);