简体   繁体   English

在抽象类中实现compareTo()

[英]Implementing compareTo() in an abstract class

I have an abstract class in my application (let's call it BaseRule ). 我的应用程序中有一个抽象类(我们称之为BaseRule )。 BaseRule implements the Comparable interface, and contains an abstract method signature public abstract int getExecOrder() . BaseRule实现Comparable接口,并包含一个抽象方法签名public abstract int getExecOrder()

Within the BaseRule implementation of the compareTo() method in the BaseRule class, the return value of getExecOrder() is used to compare the objects. BaseRule类中compareTo()方法的BaseRule实现中, getExecOrder()的返回值用于比较对象。

At present, I have 3 concrete implementations of the BaseRule class, each of which implement the getExecOrder() method 目前,我有3个BaseRule类的具体实现,每个实现getExecOrder()方法。

The ultimate purpose of having the BaseRule class implement the Comparable interface is that a Collection of BaseRule objects are passed to a utility that needs to sort these objects to ensure that they are executed in the proper order. 使BaseRule类实现Comparable接口的最终目的是将BaseRule对象的集合传递给实用程序,该实用程序需要对这些对象进行排序以确保以正确的顺序执行它们。

In this case, execution order only matters at the Class level, meaning that every BaseRuleA must be executed before any BaseRuleB is executed, but every BaseRuleB is effectively 'equal' to every other BaseRuleB , so the order that those BaseRuleB objects are processed does not matter 在这种情况下,执行顺序仅在类级别上BaseRuleA ,这意味着必须在执行任何BaseRuleB之前执行每个BaseRuleB ,但是每个BaseRuleB实际上都与其他BaseRuleB “相等”,因此处理这些BaseRuleB对象的顺序并不重要物

My question is, is there a better way to handle comparing these objects as opposed to using (what I came up with) a getExecOrder() method ? 我的问题是,有没有比使用getExecOrder()方法更好的方法来比较这些对象?

Right now, I only have 3 concrete implementations of BaseRule so it was easy enough to just assign return values of 1,2,3 to those getExecOrder() method calls, but if another developer adds a new BaseRule implementation, they would have to scan all the existing implementations, and (possibly) update those return values in all of the classes to accommodate the new BaseRule . 现在,我只有3种BaseRule具体实现,因此只需将1,2,3的返回值分配给那些getExecOrder()方法调用就足够了,但是如果另一个开发人员添加了新的BaseRule实现,则他们必须进行扫描所有现有的实现,并(可能)更新所有类中的那些返回值以适应新的BaseRule

Any ideas on a better implementation? 有更好的实施方案吗?

I guess you're talking about the following sort of thing? 我猜您在谈论以下问题?

abstract class BaseRule implements Comparable<BaseRule>
{
    abstract int executionPrecedence();

    @Override
    public int compareTo(final BaseRule that)
    {
        return Integer.compare(this.executionPrecedence(), that.executionPrecedence());
    }
}

class RuleA extends BaseRule
{
    @Override
    int executionPrecedence() { return 0; }
}

class RuleB extends BaseRule
{
    @Override
    int executionPrecedence() { return 1; }
}

It seems fairly reasonable. 看来还算合理。 If you're concerned about having to change the values of existing classes in the future then just leave big gaps rather than using contiguous integers. 如果您担心将来必须更改现有类的值,则只需留下很大的空白,而不要使用连续的整数。

A = 10000 A = 10000

B = 20000 B = 20000

C = 30000 C = 30000

Now you have 9999 spaces to place future implementations between these ones. 现在您有9999个空间可在这些空间之间放置将来的实现。


Maybe also add a unit test which uses reflection to check that no two implementations share the same precedence. 也许还添加了一个单元测试,该单元测试使用反射来检查没有两个实现共享相同的优先级。

I honestly think your proposed implementation is the best way to go about this. 老实说,我认为您建议的实现是实现此目标的最佳方法。 Definitely add some comments to the abstract getExecOrder() so that future developers know exactly what their implementation of it is supposed to do. 绝对在抽象的getExecOrder()添加一些注释,以便将来的开发人员确切地知道他们的实现应该执行的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM