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

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;