参考
掘金:规格模式(Specification Pattern)
作者:Tom弹架构
dddsample-core源码参考:github.com/citerus/ddd…
定义
规格模式(Specification Pattern)可以认为是组合模式的一种扩展。很多时候程序中的某些条件决定了业务逻辑,这些条件就可以抽离出来以某种关系(与、或、非)进行组合,从而灵活地对业务逻辑进行定制。另外,在查询、过滤等应用场合中,通过预定义多个条件,然后使用这些条件的组合来处理查询或过滤,而不是使用逻辑判断语句来处理,可以简化整个实现逻辑。 这里的每个条件都是一个规格,多个规格(条件)通过串联的方式以某种逻辑关系形成一个组合式的规格。规格模式属于结构型设计模式。
应用场景
-
验证对象,检验对象本身是否满足某些业务要求或者是否已经为实现某个业务目标做好了准备。
-
从集合中选择符合特定业务规则的对象或对象子集。
-
指定在创建新对象的时候必须要满足某种业务要求。
UML图
由上图可以看到,规格模式主要包含6个角色。
(1)抽象规格书(Specification):对规格书的抽象定义。
(2)组合规格书(CompositeSpecification):一般设计为抽象类,对规格书进行与或非操作,实现and()、or()、not()方法,在方法中关联子类,因为子类为固定类,所以父类可以进行关联。
(3)与规格书(AndSpecification):对规格书进行与操作,实现isSatisfiedBy()方法。
(4)或规格书(OrSpecification):对规格书进行或操作,实现isSatisfiedBy()方法。
(5)非规格书(NotSpecification):对规格书进行非操作,实现isSatisfiedBy()方法。
(6)业务规格书(BizSpecification):实现isSatisfiedBy()方法,对业务进行判断,一个类为一种判断方式,可进行扩展。
优点
规格模式非常巧妙地实现了对象筛选功能,适合在多个对象中筛选查找,或者业务规则不适于放在任何已有实体或值对象中,而且规则变化和组合会掩盖对象的基本含义的情况。
缺点
规格模式中有一个很严重的问题就是父类依赖子类,这种情景只有在非常明确不会发生变化的场景中存在,它不具备扩展性,是一种固化而不可变化的结构。一般在面向对象设计中应该尽量避免。
代码实现
Specificaiton、AbstractSpecification、AndSpecification、NotSpecification、OrSpecification这几个类均从 github.com/citerus/ddd… 项目中Copy出来,可直接复制到项目中使用。
Specificaiton
/**
* Specificaiton interface.
* <p/>
* Use {@link AbstractSpecification} as base for creating specifications, and
* only the method {@link #isSatisfiedBy(Object)} must be implemented.
*/
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);
}
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);
}
}
AndSpecification
/**
* AND specification, used to create a new specifcation that is the AND of two other specifications.
*/
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);
}
}
NotSpecification
/**
* NOT decorator, used to create a new specifcation that is the inverse (NOT) of the given spec.
*/
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);
}
}
OrSpecification
/**
* OR specification, used to create a new specifcation that is the OR of two other specifications.
*/
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);
}
}
业务Specification类
要判断是否满足某个条件,继承AbstractSpecification,重写isSatisfiedBy方法,在isSatisfiedBy内写判断逻辑即可。
- Test1Specification
@Component
public class Test1Specification extends AbstractSpecification<List<String>> {
@Override
public boolean isSatisfiedBy(List<String> strings) {
for (String string : strings) {
if (string.contains("a")) {
throw new IllegalArgumentException("不能包含a");
}
}
return true;
}
}
- Test2Specification
@Component
public class Test2Specification extends AbstractSpecification<List<String>> {
@Override
public boolean isSatisfiedBy(List<String> strings) {
if (strings.size() == 2){
throw new IllegalArgumentException("长度不能等于2");
}
return true;
}
}
调用
@Autowired
private Test1Specification test1Specification;
@Autowired
private Test2Specification test2Specification;
@Test
public void isSatisfiedBy() {
List<String> list = Arrays.asList("bc", "cd","ef");
boolean satisfiedBy = test1Specification.and(test2Specification).isSatisfiedBy(list);
System.out.println(satisfiedBy);
}