#1021 style changes for Specification
This commit is contained in:
parent
1d4a7681e2
commit
a8c7771784
@ -23,41 +23,44 @@
|
|||||||
|
|
||||||
package com.iluwatar.specification.app;
|
package com.iluwatar.specification.app;
|
||||||
|
|
||||||
import com.iluwatar.specification.creature.*;
|
import com.iluwatar.specification.creature.Creature;
|
||||||
|
import com.iluwatar.specification.creature.Dragon;
|
||||||
|
import com.iluwatar.specification.creature.Goblin;
|
||||||
|
import com.iluwatar.specification.creature.KillerBee;
|
||||||
|
import com.iluwatar.specification.creature.Octopus;
|
||||||
|
import com.iluwatar.specification.creature.Shark;
|
||||||
|
import com.iluwatar.specification.creature.Troll;
|
||||||
import com.iluwatar.specification.property.Color;
|
import com.iluwatar.specification.property.Color;
|
||||||
import com.iluwatar.specification.property.Movement;
|
import com.iluwatar.specification.property.Movement;
|
||||||
import com.iluwatar.specification.selector.ColorSelector;
|
import com.iluwatar.specification.selector.ColorSelector;
|
||||||
import com.iluwatar.specification.selector.MovementSelector;
|
import com.iluwatar.specification.selector.MovementSelector;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.stream.Collectors;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* <p>The central idea of the Specification pattern is to separate the statement of how to match a
|
||||||
* The central idea of the 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
|
* 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.
|
* selection, it is also valuable for validation and for building to order.</p>
|
||||||
* <p>
|
|
||||||
* 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.
|
|
||||||
* <p>
|
|
||||||
* http://martinfowler.com/apsupp/spec.pdf
|
|
||||||
*
|
*
|
||||||
|
* <p>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.</p>
|
||||||
|
*
|
||||||
|
* <p>http://martinfowler.com/apsupp/spec.pdf</p>
|
||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
private static final Logger LOGGER = LoggerFactory.getLogger(App.class);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Program entry point
|
* Program entry point.
|
||||||
*/
|
*/
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
// initialize creatures list
|
// initialize creatures list
|
||||||
List<Creature> creatures =
|
List<Creature> creatures = List.of(new Goblin(), new Octopus(), new Dragon(), new Shark(),
|
||||||
List.of(new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(), new KillerBee());
|
new Troll(), new KillerBee());
|
||||||
// find all walking creatures
|
// find all walking creatures
|
||||||
LOGGER.info("Find all walking creatures");
|
LOGGER.info("Find all walking creatures");
|
||||||
List<Creature> walkingCreatures =
|
List<Creature> walkingCreatures =
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Base class for concrete creatures.
|
* Base class for concrete creatures.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public abstract class AbstractCreature implements Creature {
|
public abstract class AbstractCreature implements Creature {
|
||||||
|
|
||||||
@ -40,7 +38,7 @@ public abstract class AbstractCreature implements Creature {
|
|||||||
private Color color;
|
private Color color;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructor
|
* Constructor.
|
||||||
*/
|
*/
|
||||||
public AbstractCreature(String name, Size size, Movement movement, Color color) {
|
public AbstractCreature(String name, Size size, Movement movement, Color color) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Creature interface.
|
* Creature interface.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public interface Creature {
|
public interface Creature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Dragon creature.
|
* Dragon creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Dragon extends AbstractCreature {
|
public class Dragon extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Goblin creature.
|
* Goblin creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Goblin extends AbstractCreature {
|
public class Goblin extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* KillerBee creature.
|
* KillerBee creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class KillerBee extends AbstractCreature {
|
public class KillerBee extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Octopus creature.
|
* Octopus creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Octopus extends AbstractCreature {
|
public class Octopus extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Shark creature.
|
* Shark creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Shark extends AbstractCreature {
|
public class Shark extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -28,9 +28,7 @@ import com.iluwatar.specification.property.Movement;
|
|||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Troll creature.
|
* Troll creature.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class Troll extends AbstractCreature {
|
public class Troll extends AbstractCreature {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.specification.property;
|
package com.iluwatar.specification.property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
* <p>Color property.</p>
|
||||||
* Color property.
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public enum Color {
|
public enum Color {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.specification.property;
|
package com.iluwatar.specification.property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Movement property.
|
* Movement property.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public enum Movement {
|
public enum Movement {
|
||||||
|
|
||||||
|
@ -24,9 +24,7 @@
|
|||||||
package com.iluwatar.specification.property;
|
package com.iluwatar.specification.property;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Size property.
|
* Size property.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public enum Size {
|
public enum Size {
|
||||||
|
|
||||||
|
@ -23,26 +23,23 @@
|
|||||||
|
|
||||||
package com.iluwatar.specification.selector;
|
package com.iluwatar.specification.selector;
|
||||||
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import com.iluwatar.specification.creature.Creature;
|
import com.iluwatar.specification.creature.Creature;
|
||||||
import com.iluwatar.specification.property.Color;
|
import com.iluwatar.specification.property.Color;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Color selector.
|
* Color selector.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class ColorSelector implements Predicate<Creature> {
|
public class ColorSelector implements Predicate<Creature> {
|
||||||
|
|
||||||
private final Color c;
|
private final Color color;
|
||||||
|
|
||||||
public ColorSelector(Color c) {
|
public ColorSelector(Color c) {
|
||||||
this.c = c;
|
this.color = c;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(Creature t) {
|
public boolean test(Creature t) {
|
||||||
return t.getColor().equals(c);
|
return t.getColor().equals(color);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,26 +23,23 @@
|
|||||||
|
|
||||||
package com.iluwatar.specification.selector;
|
package com.iluwatar.specification.selector;
|
||||||
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import com.iluwatar.specification.creature.Creature;
|
import com.iluwatar.specification.creature.Creature;
|
||||||
import com.iluwatar.specification.property.Movement;
|
import com.iluwatar.specification.property.Movement;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Movement selector.
|
* Movement selector.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class MovementSelector implements Predicate<Creature> {
|
public class MovementSelector implements Predicate<Creature> {
|
||||||
|
|
||||||
private final Movement m;
|
private final Movement movement;
|
||||||
|
|
||||||
public MovementSelector(Movement m) {
|
public MovementSelector(Movement m) {
|
||||||
this.m = m;
|
this.movement = m;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(Creature t) {
|
public boolean test(Creature t) {
|
||||||
return t.getMovement().equals(m);
|
return t.getMovement().equals(movement);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,26 +23,23 @@
|
|||||||
|
|
||||||
package com.iluwatar.specification.selector;
|
package com.iluwatar.specification.selector;
|
||||||
|
|
||||||
import java.util.function.Predicate;
|
|
||||||
|
|
||||||
import com.iluwatar.specification.creature.Creature;
|
import com.iluwatar.specification.creature.Creature;
|
||||||
import com.iluwatar.specification.property.Size;
|
import com.iluwatar.specification.property.Size;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* Size selector.
|
* Size selector.
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class SizeSelector implements Predicate<Creature> {
|
public class SizeSelector implements Predicate<Creature> {
|
||||||
|
|
||||||
private final Size s;
|
private final Size size;
|
||||||
|
|
||||||
public SizeSelector(Size s) {
|
public SizeSelector(Size s) {
|
||||||
this.s = s;
|
this.size = s;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean test(Creature t) {
|
public boolean test(Creature t) {
|
||||||
return t.getSize().equals(s);
|
return t.getSize().equals(size);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user