简体   繁体   English

如何在实现接口的所有类中设计公共静态方法

[英]How to design a common static method in all classes implementing an interface

I have an interface called Relation , implemented by a class BasicRelation , and extended by subclasses (eg ParentChild , Sibling , Spouse ). 我有一个名为Relation的接口,由BasicRelationBasicRelation ,并由子类扩展(例如ParentChildSiblingSpouse )。 While developing my code, I realized that I often need a method which takes a String representation of a relation to create it. 在开发我的代码时,我意识到我经常需要一个方法,它使用关系的String表示来创建它。 For example: 例如:

public class ParentChild implements Relation extends BasicRelation {

  // e.g. "Jack is Emily's father. Jill is her mother." will return the list
  // <ParentChild(Jack, Emily), ParentChild(Jill, Emily)>
  static List<ParentChild> fromSentence(String s) {
    ...
  }
}

Now, since I find myself needing this method ( fromSentence(String) ) in every class, except perhaps in BasicRelation , I would like to move it up the hierarchy. 现在,因为我发现自己需要这种方法( fromSentence(String)在每一个类),也许除了BasicRelation ,我想它向上的层次结构。 The problem is that the internal details of the method is subclass-dependent, so I can't have it as a static method in the interface Relation or the superclass BasicRelation . 问题是该方法的内部细节依赖于子类,因此我不能将它作为接口Relation或超类BasicRelationstatic方法。

Unfortunately, in Java, it is also not possible to have a static abstract method. 不幸的是,在Java中,也不可能有static abstract方法。

Is there any way to ensure that every subclass of BasicRelation (or every class implementing Relation ) implements fromSentence(String) ? 有没有办法确保BasicRelation每个子类(或实现Relation每个类)都实现fromSentence(String) If no, should I be designing this in a completely different way? 如果不是,我应该以完全不同的方式设计它吗? I guess this last question is more of a request for design-advice than a question. 我想最后一个问题更多的是对设计建议的要求而不是问题。

Why does the static method need to be in the interface? 为什么静态方法需要在接口中? What's stopping you from having a 'Utility' class and having the method in there? 什么阻止你拥有一个“实用工具”课程并在那里有方法?

public class RelationUtility {
    public static BasicRelation relationFactory(String asString) {
        ....
    }
}

As a static method, there is no reason other than access to private members, which can also be accomplished by by 'default' permissions on those members.... 作为一种静态方法,除了访问私有成员之外没有任何理由,这也可以通过对这些成员的“默认”权限来实现....

You can try making the BasicRelation class an abstract class and use an abstract fromSentence(..) method. 您可以尝试将BasicRelation类作为抽象类,并使用抽象fromSentence(..)方法。 This would require the ParentChild class to override and implement the fromSentence method because you can't create an object for ParentChild without implementing fromSentence() 这将要求ParentChild类覆盖并实现fromSentence方法,因为如果不实现fromSentence(),则无法为ParentChild创建对象

public abstract class BasicRelation extends Relation(){

   public abstract List<..> fromSentence(String s);
}

public class ParentChild implements Relation extends BasicRelation {

 fromSentence(){
 //parentChild class's implementation
}

} }

If I understood right... you can try an approach like this 如果我理解正确......你可以尝试这样的方法

public class BasicRelation {

    public abstract List<ParentChild> fromSentenceInSubclass(s);

    public List<ParentChild> fromSentence(String s){
        fromSentenceInSubclass(s);
    }
}

And then you could have: 然后你可以:

public class SubclassRelation extends BasicRelation {

    public List<ParentChild> fromSentenceInSubclass(s){
        // do subclass relation stuff
    }

}

You will probably need to change the code a bit and add some Generics around to make it happen the way you want. 您可能需要稍微更改代码并添加一些Generics以使其按您希望的方式发生。 Sotirios Delimanolis Factory suggestion might also be an option. Sotirios Delimanolis Factory建议也许是一种选择。

You can have the abstract class BasicRelation include the static method which throws an Exception. 你可以让抽象类BasicRelation包含抛出异常的静态方法。 That way you will be forced to override (shadow) the static method in the subclasses when you use it. 这样,当您使用它时,您将被迫覆盖(影子)子类中的静态方法。

Something like: 就像是:

public abstract class BasicRelation {
    public static List<..> fromSentence(String s) {
        throw new RuntimeException();
    }
}

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

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