实现 NotSpecification: isSpecialCaseOf 的好方法?

发布于 2024-08-28 00:44:52 字数 490 浏览 8 评论 0原文

我正在实施规范模式。 NotSpecification 乍一看似乎很简单:

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

但它并不适用于所有规范:

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

这应该返回 false 而不是 true。到目前为止,我认为完成 isSpecialCaseOf NotSpecification 的唯一方法是实现remainingUnsatisfiedBy(论文中关于规范模式的部分包含)。但也许我错过了一些更简单的东西或逻辑洞察力,使得这变得不必要。

问题:是否有另一种不使用remainingUnsatisfiedBy 的方法来实现这一点?

I'm implementing the specification pattern. The NotSpecification seems simple at first:

NotSpecification.IsSpecialCaseOf(otherSpecification)
    return !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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-09-04 00:44:52

我尝试在 Java 中实现这一点,它没有出现任何问题,也没有剩余的 UnsatisfiedBy()。可能您在实现中遇到了一些问题,这是我的:

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

问题在于 Not() 方法,该方法应该正确构造其参数的相反类型。

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

那么我需要的就是为每个规范正确实现 not() ,例如对于 LesserThan:

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

如果您有任何问题,请提供您的 GreatherThan.isSpecialCaseOf 和 Not 的实现,我会尽力提供帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文