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