Added comments and class diagram.
This commit is contained in:
@ -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());
|
||||
|
@ -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;
|
||||
|
@ -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();
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -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() {
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar.property;
|
||||
|
||||
/**
|
||||
*
|
||||
* Color property.
|
||||
*
|
||||
*/
|
||||
public enum Color {
|
||||
|
||||
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.iluwatar.property;
|
||||
|
||||
/**
|
||||
*
|
||||
* Movement property.
|
||||
*
|
||||
*/
|
||||
public enum Movement {
|
||||
|
||||
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");
|
||||
|
@ -2,7 +2,7 @@ package com.iluwatar.property;
|
||||
|
||||
/**
|
||||
*
|
||||
* Enumeration for creature size.
|
||||
* Size property.
|
||||
*
|
||||
*/
|
||||
public enum Size {
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
Reference in New Issue
Block a user