本篇文章给大家分享的是有关dddsample-core中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方法
/**
* 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方法
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)
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)
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接口。
以上就是dddsample-core中Specification有什么用,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/go4it/blog/5015642