关于java:聊聊dddsamplecore的Specification

42次阅读

共计 3421 个字符,预计需要花费 9 分钟才能阅读完成。

本文次要钻研一下 dddsample-core 的 Specification

Specification

public interface Specification<T> {

  /**
   * Check if {@code t} is satisfied by the specification.
   *
   * @param t Object to test.
   * @return {@code true} if {@code t} satisfies the specification.
   */
  boolean isSatisfiedBy(T t);

  /**
   * Create a new specification that is the AND operation of {@code this} specification and another specification.
   * @param specification Specification to AND.
   * @return A new specification.
   */
  Specification<T> and(Specification<T> specification);

  /**
   * Create a new specification that is the OR operation of {@code this} specification and another specification.
   * @param specification Specification to OR.
   * @return A new specification.
   */
  Specification<T> or(Specification<T> specification);

  /**
   * Create a new specification that is the NOT operation of {@code this} specification.
   * @param specification Specification to NOT.
   * @return A new specification.
   */
  Specification<T> not(Specification<T> specification);
}

Specification 接口定义了 isSatisfiedBy、and、or、not 办法

AbstractSpecification

/**
 * Abstract base implementation of composite {@link Specification} with default
 * implementations for {@code and}, {@code or} and {@code not}.
 */
public abstract class AbstractSpecification<T> implements Specification<T> {

  /**
   * {@inheritDoc}
   */
  public abstract boolean isSatisfiedBy(T t);

  /**
   * {@inheritDoc}
   */
  public Specification<T> and(final Specification<T> specification) {return new AndSpecification<T>(this, specification);
  }

  /**
   * {@inheritDoc}
   */
  public Specification<T> or(final Specification<T> specification) {return new OrSpecification<T>(this, specification);
  }

  /**
   * {@inheritDoc}
   */
  public Specification<T> not(final Specification<T> specification) {return new NotSpecification<T>(specification);
  }
}

AbstractSpecification 申明实现 Specification,它实现了 and、or、not 办法

AndSpecification

public class AndSpecification<T> extends AbstractSpecification<T> {

  private Specification<T> spec1;
  private Specification<T> spec2;

  /**
   * Create a new AND specification based on two other spec.
   *
   * @param spec1 Specification one.
   * @param spec2 Specification two.
   */
  public AndSpecification(final Specification<T> spec1, final Specification<T> spec2) {
    this.spec1 = spec1;
    this.spec2 = spec2;
  }

  /**
   * {@inheritDoc}
   */
  public boolean isSatisfiedBy(final T t) {return spec1.isSatisfiedBy(t) && spec2.isSatisfiedBy(t);
  }
}

AndSpecification 继承了 AbstractSpecification,它定义了 spec1、spec2 属性,其 isSatisfiedBy 返回的是 spec1.isSatisfiedBy(t) && spec2.isSatisfiedBy(t)

OrSpecification

public class OrSpecification<T> extends AbstractSpecification<T> {

  private Specification<T> spec1;
  private Specification<T> spec2;

  /**
   * Create a new OR specification based on two other spec.
   *
   * @param spec1 Specification one.
   * @param spec2 Specification two.
   */
  public OrSpecification(final Specification<T> spec1, final Specification<T> spec2) {
    this.spec1 = spec1;
    this.spec2 = spec2;
  }

  /**
   * {@inheritDoc}
   */
  public boolean isSatisfiedBy(final T t) {return spec1.isSatisfiedBy(t) || spec2.isSatisfiedBy(t);
  }
}

OrSpecification 继承了 AbstractSpecification,它定义了 spec1、spec2 属性,其 isSatisfiedBy 返回的是 spec1.isSatisfiedBy(t) || spec2.isSatisfiedBy(t)

NotSpecification

public class NotSpecification<T> extends AbstractSpecification<T> {

  private Specification<T> spec1;

  /**
   * Create a new NOT specification based on another spec.
   *
   * @param spec1 Specification instance to not.
   */
  public NotSpecification(final Specification<T> spec1) {this.spec1 = spec1;}

  /**
   * {@inheritDoc}
   */
  public boolean isSatisfiedBy(final T t) {return !spec1.isSatisfiedBy(t);
  }
}

NotSpecification 继承了 AbstractSpecification,它定义了 spec1 属性,其 isSatisfiedBy 返回的是!spec1.isSatisfiedBy(t)

小结

dddsample-core 的 Specification 接口定义了 isSatisfiedBy、and、or、not 办法;AndSpecification、OrSpecification、NotSpecification 继承了 AbstractSpecification,实现了 Specification 接口。

doc

  • dddsample-core

正文完
 0