Movatterモバイル変換


[0]ホーム

URL:


Skip to main content
Java Design Patterns

    Specification Pattern in Java: Enhancing Business Rules with Decoupled Logic

    BehavioralBusinessDomainEncapsulationEnterprise patternsExtensibilityAbout 5 min

    On This Page

    Also known as

    • Filter
    • Criteria

    Intent of Specification Design Pattern

    Encapsulate business rules and criteria that an object must satisfy to enable checking these rules in various parts of the application.

    Detailed Explanation of Specification Pattern with Real-World Examples

    Real-world example

    Imagine you are organizing a conference and need to filter attendees based on specific criteria such as registration status, payment completion, and session interests.

    Using the Specification design pattern, you would create separate specifications for each criterion (e.g., "IsRegistered", "HasPaid", "IsInterestedInSessionX"). These specifications can be combined dynamically to filter attendees who meet all the required criteria, such as those who are registered, have completed their payment, and are interested in a particular session. This approach allows for flexible and reusable business rules, ensuring that the filtering logic can be easily adjusted as needed without changing the underlying attendee objects.

    In plain words

    The Specification design pattern in Java enables the efficient encapsulation and reuse of business rules, offering a flexible and dynamic way to combine criteria for robust software development

    Wikipedia says

    In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic.

    Flowchart

    Specification Pattern flowchart
    Specification Pattern flowchart

    Programmatic Example of Specification Pattern in Java

    Let's consider a creature pool example. We have a collection of creatures with specific properties. These properties might belong to a predefined, limited set (represented by enums likeSize,Movement, andColor) or they might be continuous values (e.g., the mass of aCreature). In cases with continuous values, it's better to use a "parameterized specification," where the property value is provided as an argument when theCreature is instantiated, allowing for greater flexibility. Additionally, predefined and/or parameterized properties can be combined using boolean logic, offering almost limitless selection possibilities (this is known as a "composite specification," explained further below). The advantages and disadvantages of each approach are detailed in the table at the end of this document.

    First, here is interfaceCreature.

    public interface Creature {    String getName();    Size getSize();    Movement getMovement();    Color getColor();    Mass getMass();}

    AndDragon implementation looks like this.

    public class Dragon extends AbstractCreature {  public Dragon() {    super("Dragon",Size.LARGE,Movement.FLYING,Color.RED,new Mass(39300.0));  }}

    Now that we want to select some subset of them, we use selectors. To select creatures that fly, we should useMovementSelector. The snippet also shows the base classAbstractSelector.

    public abstract class AbstractSelector<T> implements Predicate<T> {    public AbstractSelector<T> and(AbstractSelector<T>other) {        return new ConjunctionSelector<>(this, other);    }    public AbstractSelector<T> or(AbstractSelector<T>other) {        return new DisjunctionSelector<>(this, other);    }    public AbstractSelector<T> not() {        return new NegationSelector<>(this);    }}
    public class MovementSelector extends AbstractSelector<Creature> {  private final Movement movement;  public MovementSelector(Movement m) {    this.movement = m;  }  @Override  public boolean test(Creature t) {    return t.getMovement().equals(movement);  }}

    On the other hand, when selecting creatures heavier than a chosen amount, we useMassGreaterThanSelector.

    public class MassGreaterThanSelector extends AbstractSelector<Creature> {  private final Mass mass;  public MassGreaterThanSelector(double mass) {    this.mass = new Mass(mass);  }  @Override  public boolean test(Creature t) {    return t.getMass().greaterThan(mass);  }}

    With these building blocks in place, we can perform some searches.

    @Slf4jpublic class App {  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()    );    // so-called "hard-coded" specification    LOGGER.info("Demonstrating hard-coded specification :");    // find all walking creatures    LOGGER.info("Find all walking creatures");    print(creatures,new MovementSelector(Movement.WALKING));    // find all dark creatures    LOGGER.info("Find all dark creatures");    print(creatures,new ColorSelector(Color.DARK));    LOGGER.info("\n");    // so-called "parameterized" specification    LOGGER.info("Demonstrating parameterized specification :");    // find all creatures heavier than 500kg    LOGGER.info("Find all creatures heavier than 600kg");    print(creatures,new MassGreaterThanSelector(600.0));    // find all creatures heavier than 500kg    LOGGER.info("Find all creatures lighter than or weighing exactly 500kg");    print(creatures,new MassSmallerThanOrEqSelector(500.0));    LOGGER.info("\n");    // so-called "composite" specification    LOGGER.info("Demonstrating composite specification :");    // find all red and flying creatures    LOGGER.info("Find all red and flying creatures");    var redAndFlying = new ColorSelector(Color.RED).and(new MovementSelector(Movement.FLYING));    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)));    print(creatures, scaryCreaturesSelector);  }  private static void print(List<? extends Creature>creatures,Predicate<Creature>selector) {    creatures.stream().filter(selector).map(Objects::toString).forEach(LOGGER::info);  }}

    Console output:

    12:49:24.808 [main] INFO com.iluwatar.specification.app.App -- Demonstrating hard-coded specification :12:49:24.810 [main] INFO com.iluwatar.specification.app.App -- Find all walking creatures12:49:24.812 [main] INFO com.iluwatar.specification.app.App -- Goblin [size=small, movement=walking, color=green, mass=30.0kg]12:49:24.812 [main] INFO com.iluwatar.specification.app.App -- Troll [size=large, movement=walking, color=dark, mass=4000.0kg]12:49:24.812 [main] INFO com.iluwatar.specification.app.App -- Find all dark creatures12:49:24.815 [main] INFO com.iluwatar.specification.app.App -- Octopus [size=normal, movement=swimming, color=dark, mass=12.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Troll [size=large, movement=walking, color=dark, mass=4000.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App --12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Demonstrating parameterized specification :12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Find all creatures heavier than 600kg12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Dragon [size=large, movement=flying, color=red, mass=39300.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Troll [size=large, movement=walking, color=dark, mass=4000.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Find all creatures lighter than or weighing exactly 500kg12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Goblin [size=small, movement=walking, color=green, mass=30.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Octopus [size=normal, movement=swimming, color=dark, mass=12.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Shark [size=normal, movement=swimming, color=light, mass=500.0kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- KillerBee [size=small, movement=flying, color=light, mass=6.7kg]12:49:24.816 [main] INFO com.iluwatar.specification.app.App --12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Demonstrating composite specification :12:49:24.816 [main] INFO com.iluwatar.specification.app.App -- Find all red and flying creatures12:49:24.817 [main] INFO com.iluwatar.specification.app.App -- Dragon [size=large, movement=flying, color=red, mass=39300.0kg]12:49:24.817 [main] INFO com.iluwatar.specification.app.App -- Find all scary creatures12:49:24.818 [main] INFO com.iluwatar.specification.app.App -- Dragon [size=large, movement=flying, color=red, mass=39300.0kg]12:49:24.818 [main] INFO com.iluwatar.specification.app.App -- Troll [size=large, movement=walking, color=dark, mass=4000.0kg]

    Adopting the Specification pattern significantly enhances the flexibility and reusability of business rules within Java applications, contributing to more maintainable code.

    When to Use the Specification Pattern in Java

    Apply the Java Specification pattern when

    • You need to filter objects based on different criteria.
    • The filtering criteria can change dynamically.
    • Ideal for use cases involving complex business rules that must be reused across different parts of an application.

    Real-World Applications of Specification Pattern in Java

    • Validating user inputs in enterprise applications.
    • Filtering search results in e-commerce applications.
    • Business rule validation in domain-driven design (DDD).

    Benefits and Trade-offs of Specification Pattern

    Benefits:

    • Enhances the flexibility and reusability of business rules.
    • Promotessingle responsibility principle by separating business rules from the entities.
    • Facilitates unit testing of business rules.

    Trade-offs:

    • Can lead to a proliferation of small classes, increasing complexity.
    • Might introduce performance overhead due to the dynamic checking of specifications.

    Related Java Design Patterns

    • Composite: Often used together with Specification to combine multiple specifications.
    • Decorator: Can be used to add additional criteria to a specification dynamically.
    • Strategy: Both patterns involve encapsulating a family of algorithms. Strategy encapsulates different strategies or algorithms, while Specification encapsulates business rules.

    References and Credits


    [8]ページ先頭

    ©2009-2025 Movatter.jp