Good way to implement NotSpecification: isSpecialCaseOf?

I am implementing a specification template. At first, the NotSpecification function seems simple:

NotSpecification.IsSpecialCaseOf(otherSpecification) return !this.specification.isSpecialCaseOf(otherSpecification) 

But this does not work for all specifications:

 Not(LesserThan(4)).IsSpecialCaseOf(Equals(5)) 

This should return false instead of true. So far, I believe that the only way to perform isSpecialCaseOf NotSpecification is to implement the remainder of UnsatisfiedBy (partial submission in a document by specification template). But perhaps I am missing something more simple or logical understanding that makes this unnecessary.

Question: Is there any other way to implement this without using the remainder? Unsatisfactory? <? >

+4
source share
1 answer

I tried to implement this in Java, and it went through without problems and remained unsatisfied with By (). You probably have some problems in your implementation, here's mine:

 public boolean isSpecialCaseOf(Specification spec) { if (spec instanceof GreaterThan) { return ((GreaterThan) spec).boundary > this.boundary; } return false; } 

The trap is in the Not () method, which should correctly construct the opposite type of argument.

 static final Specification Not(Specification spec) { return spec.not(); } 

Then I only need to have the correct implementation of not () for each specification, for example. for LesserThan:

  @Override public Specification not() { return new GreaterThan(boundary); } 

If you have any problems, please provide your implementation of GreatherThan.isSpecialCaseOf and not, I will try to help.

+3
source

Source: https://habr.com/ru/post/1306285/


All Articles