Added comments and class diagram.

This commit is contained in:
Ilkka Seppala 2015-04-23 22:51:21 +03:00
parent cd581154ac
commit fcd500d3ae
17 changed files with 292 additions and 1 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 49 KiB

View File

@ -0,0 +1,212 @@
<?xml version="1.0" encoding="UTF-8"?>
<class-diagram version="1.1.8" icons="true" automaticImage="PNG" always-add-relationships="false" generalizations="true"
realizations="true" associations="true" dependencies="false" nesting-relationships="true">
<class id="1" language="java" name="com.iluwatar.creature.KillerBee" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/KillerBee.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="127" x="535" y="361"/>
<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"/>
</display>
</class>
<enumeration id="2" language="java" name="com.iluwatar.property.Size" project="specification"
file="/specification/src/main/java/com/iluwatar/property/Size.java" binary="false" corner="BOTTOM_RIGHT">
<position height="178" width="144" x="351" y="361"/>
<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"/>
</display>
</enumeration>
<class id="3" language="java" name="com.iluwatar.creature.Troll" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Troll.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="127" x="702" y="361"/>
<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"/>
</display>
</class>
<class id="4" language="java" name="com.iluwatar.selector.SizeSelector" project="specification"
file="/specification/src/main/java/com/iluwatar/selector/SizeSelector.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="154" x="389" y="126"/>
<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"/>
</display>
</class>
<class id="5" language="java" name="com.iluwatar.selector.MovementSelector" project="specification"
file="/specification/src/main/java/com/iluwatar/selector/MovementSelector.java" binary="false" corner="BOTTOM_RIGHT">
<position height="106" width="200" x="149" y="126"/>
<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"/>
</display>
</class>
<class id="6" language="java" name="com.iluwatar.creature.Shark" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Shark.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="127" x="869" y="361"/>
<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"/>
</display>
</class>
<class id="7" language="java" name="com.iluwatar.creature.Dragon" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Dragon.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="127" x="1036" y="361"/>
<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"/>
</display>
</class>
<class id="8" language="java" name="com.iluwatar.creature.AbstractCreature" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/AbstractCreature.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="1101" y="222"/>
<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"/>
</display>
</class>
<class id="9" language="java" name="com.iluwatar.creature.Octopus" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Octopus.java" binary="false" corner="BOTTOM_RIGHT">
<position height="88" width="127" x="1203" y="361"/>
<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"/>
</display>
</class>
<class id="10" language="java" name="com.iluwatar.selector.ColorSelector" project="specification"
file="/specification/src/main/java/com/iluwatar/selector/ColorSelector.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="1647" y="223"/>
<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"/>
</display>
</class>
<interface id="11" language="java" name="com.iluwatar.creature.Creature" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Creature.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="717" y="223"/>
<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"/>
</display>
</interface>
<enumeration id="12" language="java" name="com.iluwatar.property.Movement" project="specification"
file="/specification/src/main/java/com/iluwatar/property/Movement.java" binary="false" corner="BOTTOM_RIGHT">
<position height="178" width="162" x="149" y="361"/>
<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"/>
</display>
</enumeration>
<class id="13" language="java" name="com.iluwatar.creature.Goblin" project="specification"
file="/specification/src/main/java/com/iluwatar/creature/Goblin.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="1447" y="405"/>
<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"/>
</display>
</class>
<enumeration id="14" language="java" name="com.iluwatar.property.Color" project="specification"
file="/specification/src/main/java/com/iluwatar/property/Color.java" binary="false" corner="BOTTOM_RIGHT">
<position height="-1" width="-1" x="1416" y="223"/>
<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"/>
</display>
</enumeration>
<generalization id="15">
<end type="SOURCE" refId="1"/>
<end type="TARGET" refId="8"/>
</generalization>
<generalization id="16">
<end type="SOURCE" refId="6"/>
<end type="TARGET" refId="8"/>
</generalization>
<generalization id="17">
<end type="SOURCE" refId="3"/>
<end type="TARGET" refId="8"/>
</generalization>
<association id="18">
<end type="SOURCE" refId="8" navigable="false">
<attribute id="19" name="size"/>
<multiplicity id="20" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="21">
<end type="SOURCE" refId="8" navigable="false">
<attribute id="22" name="movement"/>
<multiplicity id="23" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="12" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="24">
<end type="SOURCE" refId="9"/>
<end type="TARGET" refId="8"/>
</generalization>
<association id="25">
<end type="SOURCE" refId="4" navigable="false">
<attribute id="26" name="s"/>
<multiplicity id="27" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="2" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="28">
<end type="SOURCE" refId="8" navigable="false">
<attribute id="29" name="color"/>
<multiplicity id="30" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="14" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<generalization id="31">
<end type="SOURCE" refId="7"/>
<end type="TARGET" refId="8"/>
</generalization>
<realization id="32">
<end type="SOURCE" refId="8"/>
<end type="TARGET" refId="11"/>
</realization>
<generalization id="33">
<end type="SOURCE" refId="13"/>
<end type="TARGET" refId="8"/>
</generalization>
<association id="34">
<end type="SOURCE" refId="5" navigable="false">
<attribute id="35" name="m"/>
<multiplicity id="36" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="12" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<association id="37">
<end type="SOURCE" refId="10" navigable="false">
<attribute id="38" name="c"/>
<multiplicity id="39" minimum="0" maximum="1"/>
</end>
<end type="TARGET" refId="14" navigable="true"/>
<display labels="true" multiplicity="true"/>
</association>
<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

@ -16,7 +16,21 @@ import com.iluwatar.property.Movement;
import com.iluwatar.selector.ColorSelector;
import com.iluwatar.selector.MovementSelector;
/**
*
* The central idea of Specification pattern is to separate the statement of how to match a candidate, from the
* candidate object that it is matched against. As well as its usefulness in selection, it is also valuable for
* validation and for building to order.
*
* In this example we have a pool of creatures with different properties. We then have defined separate selection
* rules (Specifications) that we apply to the collection and as output receive only the creatures that match
* the selection criteria.
*
* http://martinfowler.com/apsupp/spec.pdf
*
*/
public class App {
public static void main( String[] args ) {
// initialize creatures list
List<Creature> creatures = Arrays.asList(new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(), new KillerBee());

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Base class for concrete creatures.
*
*/
public abstract class AbstractCreature implements Creature {
private String name;

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Creature interface.
*
*/
public interface Creature {
String getName();

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Dragon creature.
*
*/
public class Dragon extends AbstractCreature {
public Dragon() {

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Goblin creature.
*
*/
public class Goblin extends AbstractCreature {
public Goblin() {

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* KillerBee creature.
*
*/
public class KillerBee extends AbstractCreature {
public KillerBee() {

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Octopus creature.
*
*/
public class Octopus extends AbstractCreature {
public Octopus() {

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Shark creature.
*
*/
public class Shark extends AbstractCreature {
public Shark() {

View File

@ -4,6 +4,11 @@ import com.iluwatar.property.Color;
import com.iluwatar.property.Movement;
import com.iluwatar.property.Size;
/**
*
* Troll creature.
*
*/
public class Troll extends AbstractCreature {
public Troll() {

View File

@ -1,5 +1,10 @@
package com.iluwatar.property;
/**
*
* Color property.
*
*/
public enum Color {
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");

View File

@ -1,5 +1,10 @@
package com.iluwatar.property;
/**
*
* Movement property.
*
*/
public enum Movement {
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");

View File

@ -2,7 +2,7 @@ package com.iluwatar.property;
/**
*
* Enumeration for creature size.
* Size property.
*
*/
public enum Size {

View File

@ -5,6 +5,11 @@ import java.util.function.Predicate;
import com.iluwatar.creature.Creature;
import com.iluwatar.property.Color;
/**
*
* Color selector.
*
*/
public class ColorSelector implements Predicate<Creature> {
private final Color c;

View File

@ -5,6 +5,11 @@ import java.util.function.Predicate;
import com.iluwatar.creature.Creature;
import com.iluwatar.property.Movement;
/**
*
* Movement selector.
*
*/
public class MovementSelector implements Predicate<Creature> {
private final Movement m;

View File

@ -5,6 +5,11 @@ import java.util.function.Predicate;
import com.iluwatar.creature.Creature;
import com.iluwatar.property.Size;
/**
*
* Size selector.
*
*/
public class SizeSelector implements Predicate<Creature> {
private final Size s;