good way to implement NotSpecification: isSpecialCaseOf?
I'm implementing the specification pattern. The NotSpecification seems simple at first:
NotSpecification.IsSpecialCaseOf(otherSpecification)
r开发者_如何学编程eturn !this.specification.isSpecialCaseOf(otherSpecification)
But it doesn't work for all Specifications:
Not(LesserThan(4)).IsSpecialCaseOf(Equals(5))
This should return false instead of true. So far I think that the only way to accomplish the isSpecialCaseOf the NotSpecification is to implement the remainderUnsatisfiedBy (partial subsumption in the paper on the specification pattern). But maybe I am missing something more simple or a logical insight that makes this unnecessary.
Question: Is there another way of implementing this by not using remainderUnsatisfiedBy?
I have tried to implement this in Java and it went without problems and remainderUnsatisfiedBy(). Probably you have some problem in your implementation, here is mine:
public boolean isSpecialCaseOf(Specification spec) {
if (spec instanceof GreaterThan) {
return ((GreaterThan) spec).boundary > this.boundary;
}
return false;
}
The catch is in the Not() method, which should correctly construct opposite type of its argument.
static final Specification Not(Specification spec) {
return spec.not();
}
Then all I need is to have correct implementation of not() for every Specification, e.g. for LesserThan:
@Override
public Specification not() {
return new GreaterThan(boundary);
}
If you have any problems, please provide your implementation of GreatherThan.isSpecialCaseOf and of Not, I will try to help.
精彩评论