专栏首页码匠的流水账聊聊dddsample-core的Specification

聊聊dddsample-core的Specification

本文主要研究一下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

本文分享自微信公众号 - 码匠的流水账(geek_luandun),作者:码匠乱炖

原文出处及转载信息见文内详细说明,如有侵权,请联系 yunjia_community@tencent.com 删除。

原始发表时间:2021-04-12

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 聊聊dddsample-core的Specification

    dddsample-core的Specification接口定义了isSatisfiedBy、and、or、not方法;AndSpecification、OrS...

    codecraft
  • 聊聊dddsample-core的model

    dddsample-core定义了Entity、ValueObject、DomainEvent接口,它们分别定义了sameIdentityAs、sameValu...

    codecraft
  • 聊聊golang的zap的Core

    Core接口内嵌了LevelEnabler,定义了With、Check、Write、Sync方法;它有nopCore及ioCore两种实现,ioCore内嵌了L...

    codecraft
  • 聊聊golang的zap的Core

    Core接口内嵌了LevelEnabler,定义了With、Check、Write、Sync方法;它有nopCore及ioCore两种实现,ioCore内嵌了L...

    codecraft
  • Spring Boot & 单元测试

    十毛
  • 聊聊eureka的renewal

    eureka client在实例化的时候注册了一个定时任务,每隔renewalIntervalInSecs,向eureka server发送一次renewal。...

    codecraft
  • 聊聊storm trident spout的_maxTransactionActive

    本文主要研究一下storm trident spout的_maxTransactionActive

    codecraft
  • 聊聊storm trident spout的_maxTransactionActive

    本文主要研究一下storm trident spout的_maxTransactionActive

    codecraft
  • ASP.NET Core 2.0 : 六. 举个例子来聊聊它的依赖注入

    FlyLolo
  • ASP.NET Core 2.0 : 六. 举个例子来聊聊它的依赖注入

    FlyLolo
  • 聊聊ASP.NET Core默认提供的这个跨平台的服务器——KestrelServer

    跨平台是ASP.NET Core一个显著的特性,而KestrelServer是目前微软推出了唯一一个能够真正跨平台的Server。KestrelServer利用...

    蒋金楠
  • Rasa 聊天机器人专栏(五):模型评估

    机器学习中的标准技术是将一些数据作为测试集分开。你可以使用以下方法将NLU训练数据拆分为训练集和测试集:

    磐创AI
  • 邻里街坊 golang入坑系列

    如果要追新或者怀旧,就点击https://andy-zhangtao.gitbooks.io/golang/content/ 。 博客园里面的文章基本和gitb...

    随机来个数
  • 分布式NoSQL列存储数据库Hbase_列族的设计(五)

    ChinaManor
  • 聊聊flink的TimeCharacteristic

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/Time...

    codecraft
  • 聊聊flink的SourceFunction

    flink-streaming-java_2.11-1.6.2-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft
  • 聊聊flink的CsvReader

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/ExecutionEnvironment.jav...

    codecraft
  • 聊聊flink的BoltWrapper

    flink-storm_2.11-1.6.2-sources.jar!/org/apache/flink/storm/wrappers/BoltWrapper....

    codecraft
  • 聊聊flink的EventTime

    flink-streaming-java_2.11-1.7.0-sources.jar!/org/apache/flink/streaming/api/func...

    codecraft

扫码关注云+社区

领取腾讯云代金券