简体   繁体   English

接口方法采用与接口相同的实现

[英]Interface method taking same implementation of interface

I have the following interface : 我有以下界面:

public interface ClusterPopulation
{
    public double computeDistance(ClusterPopulation other);
}

Is it possible to specify within the interface itself, that implementation A of ClusterPopulation can only take A implementation as argument of computeDistance ? 是否可以在接口本身中指定ClusterPopulation的实现A只能将A实现作为computeDistance的参数?

The only approching solution that I see is the following, but I don't like it : 我看到的唯一解决方案如下,但我不喜欢它:

Redefine interface with generics: 用泛型重新定义接口:

public interface ClusterPopulation
{
    public <T extends ClusterPopulation> double computeDistance(T other);
}

Within the implementation, throw IllegalArgumentException if argument is not from the good type, do some casts if type is ok... Meeeeh ! 在实现中,如果参数不是来自良好类型,则抛出IllegalArgumentException;如果类型正常,则进行一些强制转换... Meeeeh!

Even with this approach, end-user is only aware of the constraint by reading the documentation/looking at code implementation/trial and error... 即使采用这种方法,最终用户也只能通过阅读文档/查看代码实现/尝试和错误来了解约束条件...

Any better solution ? 有更好的解决方案吗?

You had the right idea with using generics, but instead of applying it to the method, apply it to the whole interface. 您对使用泛型有正确的想法,但不要将其应用于方法,而应将其应用于整个接口。

public interface ClusterPopulation<T extends ClusterPopulation<T>>
{
    double computeDistance(T other);
}

That allows an implementation to define T as itself. 这允许实现将T定义为自身。

public class ClusterPopulationA implements ClusterPopulation<ClusterPopulationA> {  // ...

However, it doesn't disallow an implementation to define it as something else. 但是,这并不禁止将其定义为其他实现。

public class BreaksPattern implements ClusterPopulation<ClusterPopulationA>

Include in your documentation that all subclasses should define the type parameter T as its own class. 在您的文档中,所有子类都应将类型参数T定义为其自己的类。

In my view, there is a flaw in your design that causes problems. 我认为您的设计中存在一个缺陷,会引起问题。 From what you have provided, it seems that ClusterPopulation should be a class, not an interface. 根据您提供的内容,似乎ClusterPopulation应该是一个类,而不是接口。 Lets look at this method, 让我们看看这种方法,

public double computeDistance(ClusterPopulation other);

What does this mean? 这是什么意思? It means that an object of type ClusterPopulation is passed to this method. 这意味着将类型为ClusterPopulation的对象传递给此方法。 Furthermore, this object must have some attributes, otherwise how would you compute the distance from this object if it doesn't? 此外,该对象必须具有一些属性,否则,如何计算距该对象的距离? Combining these two observations, one can conclude that ClusterPopulation should be a class in order to have an object of that type. 结合这两个观察结果,可以得出结论,ClusterPopulation应该是一个类,以便具有该类型的对象。 When I say a class, it can be concrete or abstract. 当我说一堂课时,它可以是具体的或抽象的。 Let's take the case for abstract class. 让我们以抽象类为例。

public abstract class ClusterPopulation
{
    // common attributes, if any

    abstract public double computeDistance();
}

public class A extends ClusterPopulation {

    public double computeDistance() {
        // do some computation based on ClusterPopulation attributes

    }

}

public class B extends ClusterPopulation {

     public double computeDistance() {
        // do computation based on ClusterPopulation attributes

     }

}

Now, you would use it this way: 现在,您将以这种方式使用它:

ClusterPopulation a = new A();
ClusterPopulation b = new B();

double aResult = a.computeDistance();
double bResult = b.computeDistance();

Notice that the restriction you require is enforced here. 请注意,此处要求您执行限制。 Although both a and b are objects of type ClusterPopulation, computeDistance() works only with the concrete type of the calling object. 尽管ab都是ClusterPopulation类型的对象, 但是computeDistance()仅适用于调用对象的具体类型。

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

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