mirror of
https://github.com/tiennm99/java-design-patterns.git
synced 2026-05-15 18:59:10 +00:00
deps: Refactor dependencies (#3224)
* remove spring dep move junit, logging, mockito under dep mgmt * upgrade anti-corruption-layer deps * async method invocation * balking, bloc * bridge to bytecode * caching * callback - cqrs * component - health check * hexagonal - metadata mapping * rest of the patterns * remove checkstyle, take spotless into use
This commit is contained in:
@@ -44,32 +44,25 @@ import java.util.function.Predicate;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
/**
|
||||
* <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
|
||||
* selection, it is also valuable for validation and for building to order.</p>
|
||||
* 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>
|
||||
* only the creatures that match the selection criteria.
|
||||
*
|
||||
* <p>http://martinfowler.com/apsupp/spec.pdf</p>
|
||||
* <p>http://martinfowler.com/apsupp/spec.pdf
|
||||
*/
|
||||
@Slf4j
|
||||
public class App {
|
||||
|
||||
/**
|
||||
* Program entry point.
|
||||
*/
|
||||
/** Program entry point. */
|
||||
public static void main(String[] args) {
|
||||
// initialize creatures list
|
||||
var creatures = List.of(
|
||||
new Goblin(),
|
||||
new Octopus(),
|
||||
new Dragon(),
|
||||
new Shark(),
|
||||
new Troll(),
|
||||
new KillerBee()
|
||||
);
|
||||
var creatures =
|
||||
List.of(
|
||||
new Goblin(), new Octopus(), new Dragon(), new Shark(), new Troll(), new KillerBee());
|
||||
// so-called "hard-coded" specification
|
||||
LOGGER.info("Demonstrating hard-coded specification :");
|
||||
// find all walking creatures
|
||||
@@ -96,9 +89,11 @@ public class App {
|
||||
print(creatures, redAndFlying);
|
||||
// find all creatures dark or red, non-swimming, and heavier than or equal to 400kg
|
||||
LOGGER.info("Find all scary creatures");
|
||||
var scaryCreaturesSelector = new ColorSelector(Color.DARK)
|
||||
.or(new ColorSelector(Color.RED)).and(new MovementSelector(Movement.SWIMMING).not())
|
||||
.and(new MassGreaterThanSelector(400.0).or(new MassEqualSelector(400.0)));
|
||||
var scaryCreaturesSelector =
|
||||
new ColorSelector(Color.DARK)
|
||||
.or(new ColorSelector(Color.RED))
|
||||
.and(new MovementSelector(Movement.SWIMMING).not())
|
||||
.and(new MassGreaterThanSelector(400.0).or(new MassEqualSelector(400.0)));
|
||||
print(creatures, scaryCreaturesSelector);
|
||||
}
|
||||
|
||||
|
||||
+4
-8
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Base class for concrete creatures.
|
||||
*/
|
||||
/** Base class for concrete creatures. */
|
||||
public abstract class AbstractCreature implements Creature {
|
||||
|
||||
private final String name;
|
||||
@@ -40,9 +38,7 @@ public abstract class AbstractCreature implements Creature {
|
||||
private final Color color;
|
||||
private final Mass mass;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
/** Constructor. */
|
||||
public AbstractCreature(String name, Size size, Movement movement, Color color, Mass mass) {
|
||||
this.name = name;
|
||||
this.size = size;
|
||||
@@ -53,8 +49,8 @@ public abstract class AbstractCreature implements Creature {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return String.format("%s [size=%s, movement=%s, color=%s, mass=%s]",
|
||||
name, size, movement, color, mass);
|
||||
return String.format(
|
||||
"%s [size=%s, movement=%s, color=%s, mass=%s]", name, size, movement, color, mass);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Creature interface.
|
||||
*/
|
||||
/** Creature interface. */
|
||||
public interface Creature {
|
||||
|
||||
String getName();
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Dragon creature.
|
||||
*/
|
||||
/** Dragon creature. */
|
||||
public class Dragon extends AbstractCreature {
|
||||
|
||||
public Dragon() {
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Goblin creature.
|
||||
*/
|
||||
/** Goblin creature. */
|
||||
public class Goblin extends AbstractCreature {
|
||||
|
||||
public Goblin() {
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* KillerBee creature.
|
||||
*/
|
||||
/** KillerBee creature. */
|
||||
public class KillerBee extends AbstractCreature {
|
||||
|
||||
public KillerBee() {
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Octopus creature.
|
||||
*/
|
||||
/** Octopus creature. */
|
||||
public class Octopus extends AbstractCreature {
|
||||
|
||||
public Octopus() {
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Shark creature.
|
||||
*/
|
||||
/** Shark creature. */
|
||||
public class Shark extends AbstractCreature {
|
||||
|
||||
public Shark() {
|
||||
|
||||
@@ -29,9 +29,7 @@ import com.iluwatar.specification.property.Mass;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Troll creature.
|
||||
*/
|
||||
/** Troll creature. */
|
||||
public class Troll extends AbstractCreature {
|
||||
|
||||
public Troll() {
|
||||
|
||||
@@ -24,12 +24,12 @@
|
||||
*/
|
||||
package com.iluwatar.specification.property;
|
||||
|
||||
/**
|
||||
* Color property.
|
||||
*/
|
||||
/** Color property. */
|
||||
public enum Color {
|
||||
|
||||
DARK("dark"), LIGHT("light"), GREEN("green"), RED("red");
|
||||
DARK("dark"),
|
||||
LIGHT("light"),
|
||||
GREEN("green"),
|
||||
RED("red");
|
||||
|
||||
private final String title;
|
||||
|
||||
|
||||
@@ -26,9 +26,7 @@ package com.iluwatar.specification.property;
|
||||
|
||||
import lombok.EqualsAndHashCode;
|
||||
|
||||
/**
|
||||
* Mass property.
|
||||
*/
|
||||
/** Mass property. */
|
||||
@EqualsAndHashCode
|
||||
public class Mass {
|
||||
|
||||
@@ -60,5 +58,4 @@ public class Mass {
|
||||
public String toString() {
|
||||
return title;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -24,12 +24,11 @@
|
||||
*/
|
||||
package com.iluwatar.specification.property;
|
||||
|
||||
/**
|
||||
* Movement property.
|
||||
*/
|
||||
/** Movement property. */
|
||||
public enum Movement {
|
||||
|
||||
WALKING("walking"), SWIMMING("swimming"), FLYING("flying");
|
||||
WALKING("walking"),
|
||||
SWIMMING("swimming"),
|
||||
FLYING("flying");
|
||||
|
||||
private final String title;
|
||||
|
||||
|
||||
@@ -24,12 +24,11 @@
|
||||
*/
|
||||
package com.iluwatar.specification.property;
|
||||
|
||||
/**
|
||||
* Size property.
|
||||
*/
|
||||
/** Size property. */
|
||||
public enum Size {
|
||||
|
||||
SMALL("small"), NORMAL("normal"), LARGE("large");
|
||||
SMALL("small"),
|
||||
NORMAL("normal"),
|
||||
LARGE("large");
|
||||
|
||||
private final String title;
|
||||
|
||||
|
||||
+1
-3
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
|
||||
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* Base class for selectors.
|
||||
*/
|
||||
/** Base class for selectors. */
|
||||
public abstract class AbstractSelector<T> implements Predicate<T> {
|
||||
|
||||
public AbstractSelector<T> and(AbstractSelector<T> other) {
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Color;
|
||||
|
||||
/**
|
||||
* Color selector.
|
||||
*/
|
||||
/** Color selector. */
|
||||
public class ColorSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Color color;
|
||||
|
||||
+2
-6
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A Selector defined as the conjunction (AND) of other (leaf) selectors.
|
||||
*/
|
||||
/** A Selector defined as the conjunction (AND) of other (leaf) selectors. */
|
||||
public class ConjunctionSelector<T> extends AbstractSelector<T> {
|
||||
|
||||
private final List<AbstractSelector<T>> leafComponents;
|
||||
@@ -38,9 +36,7 @@ public class ConjunctionSelector<T> extends AbstractSelector<T> {
|
||||
this.leafComponents = List.of(selectors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if *all* selectors pass the test.
|
||||
*/
|
||||
/** Tests if *all* selectors pass the test. */
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
return leafComponents.stream().allMatch(comp -> (comp.test(t)));
|
||||
|
||||
+2
-6
@@ -26,9 +26,7 @@ package com.iluwatar.specification.selector;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* A Selector defined as the disjunction (OR) of other (leaf) selectors.
|
||||
*/
|
||||
/** A Selector defined as the disjunction (OR) of other (leaf) selectors. */
|
||||
public class DisjunctionSelector<T> extends AbstractSelector<T> {
|
||||
|
||||
private final List<AbstractSelector<T>> leafComponents;
|
||||
@@ -38,9 +36,7 @@ public class DisjunctionSelector<T> extends AbstractSelector<T> {
|
||||
this.leafComponents = List.of(selectors);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if *at least one* selector passes the test.
|
||||
*/
|
||||
/** Tests if *at least one* selector passes the test. */
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
return leafComponents.stream().anyMatch(comp -> comp.test(t));
|
||||
|
||||
+2
-6
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Mass;
|
||||
|
||||
/**
|
||||
* Mass selector for values exactly equal than the parameter.
|
||||
*/
|
||||
/** Mass selector for values exactly equal than the parameter. */
|
||||
public class MassEqualSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Mass mass;
|
||||
|
||||
/**
|
||||
* The use of a double as a parameter will spare some typing when instantiating this class.
|
||||
*/
|
||||
/** The use of a double as a parameter will spare some typing when instantiating this class. */
|
||||
public MassEqualSelector(double mass) {
|
||||
this.mass = new Mass(mass);
|
||||
}
|
||||
|
||||
+2
-6
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Mass;
|
||||
|
||||
/**
|
||||
* Mass selector for values greater than the parameter.
|
||||
*/
|
||||
/** Mass selector for values greater than the parameter. */
|
||||
public class MassGreaterThanSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Mass mass;
|
||||
|
||||
/**
|
||||
* The use of a double as a parameter will spare some typing when instantiating this class.
|
||||
*/
|
||||
/** The use of a double as a parameter will spare some typing when instantiating this class. */
|
||||
public MassGreaterThanSelector(double mass) {
|
||||
this.mass = new Mass(mass);
|
||||
}
|
||||
|
||||
+2
-6
@@ -27,16 +27,12 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Mass;
|
||||
|
||||
/**
|
||||
* Mass selector for values smaller or equal to the parameter.
|
||||
*/
|
||||
/** Mass selector for values smaller or equal to the parameter. */
|
||||
public class MassSmallerThanOrEqSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Mass mass;
|
||||
|
||||
/**
|
||||
* The use of a double as a parameter will spare some typing when instantiating this class.
|
||||
*/
|
||||
/** The use of a double as a parameter will spare some typing when instantiating this class. */
|
||||
public MassSmallerThanOrEqSelector(double mass) {
|
||||
this.mass = new Mass(mass);
|
||||
}
|
||||
|
||||
+1
-3
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Movement;
|
||||
|
||||
/**
|
||||
* Movement selector.
|
||||
*/
|
||||
/** Movement selector. */
|
||||
public class MovementSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Movement movement;
|
||||
|
||||
+1
-4
@@ -24,7 +24,6 @@
|
||||
*/
|
||||
package com.iluwatar.specification.selector;
|
||||
|
||||
|
||||
/**
|
||||
* A Selector defined as the negation (NOT) of a (leaf) selectors. This is of course only useful
|
||||
* when used in combination with other composite selectors.
|
||||
@@ -37,9 +36,7 @@ public class NegationSelector<T> extends AbstractSelector<T> {
|
||||
this.component = selector;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests if the selector fails the test (yes).
|
||||
*/
|
||||
/** Tests if the selector fails the test (yes). */
|
||||
@Override
|
||||
public boolean test(T t) {
|
||||
return !(component.test(t));
|
||||
|
||||
@@ -27,9 +27,7 @@ package com.iluwatar.specification.selector;
|
||||
import com.iluwatar.specification.creature.Creature;
|
||||
import com.iluwatar.specification.property.Size;
|
||||
|
||||
/**
|
||||
* Size selector.
|
||||
*/
|
||||
/** Size selector. */
|
||||
public class SizeSelector extends AbstractSelector<Creature> {
|
||||
|
||||
private final Size size;
|
||||
|
||||
Reference in New Issue
Block a user