应该将一个公共静态方法放在一个类还是一个接口中?

huangapple go评论61阅读模式
英文:

Should I place a public static method in a class or an interface?

问题

I wonder what considerations I should make, when I have to decide whether to take an interface or a class to place my static helper method in.

Concrete example: I want to provide a little method, that I can call from the main method in Swing components, so I can faster iterate the development on them.

Should I place it in an interface or in a class?

public final class SwingDevHelper {
    public static void startSwingContainer(Container container) {
        ...
    }
}
public interface SwingDevHelper {
    static void startSwingContainer(Container container) {
        ...
    }
}

Do I have to consider semantical or technical points, e.g. performance or visibility?

英文:

I wonder what considerations I should make, when I have to decide whether to take an interface or a class to place my static helper method in.

Concrete example: I want to provide a little method, that I can call from the main method in Swing components, so I can faster iterate the development on them.

Should I place it in an interface or in a class?

public final class SwingDevHelper {
    public static void startSwingContainer(Container container) {
        ...
    }
}
public interface SwingDevHelper {
    static void startSwingContainer(Container container) {
        ...
    }
}

Do I have to consider semantical or technical points, e.g. performance or visibility?

答案1

得分: 1

只有在SwingDevHelper的可实例性方面存在差异。

如果需要实例化SwingDevHelper,它可以是一个接口或一个(最终的)类。

如果不需要(和/或不想要)实例化SwingDevHelper,请将其定义为一个具有私有构造函数的类:

public final class SwingDevHelper {
    private SwingDevHelper() {}  // 防止在类外部实例化。

    public static void startSwingContainer(Container container) {
        ...
    }
}

使类无法实例化是一个不错的主意,如果可能的话(即当该类的实例是没有意义/无用的),因为它简化了代码的推理:您永远不必担心在遇到其实例时该如何处理。

在《Effective Java》中特别提到了这一点:在第3版中是第22条:“仅使用接口来定义类型”。简而言之,如果SwingDevHelper不是一个有意义的类型,就不要使用接口。

英文:

The difference comes in the instantiability of SwingDevHelper.

If you need to instantiate SwingDevHelper, it can be either an interface or a (final) class.

If you don't need to (and/or don't want to) instantiate SwingDevHelper, make it a class with a private constructor:

public final class SwingDevHelper {
    private SwingDevHelper() {}  // Prevents instantiation outside this class.

    public static void startSwingContainer(Container container) {
        ...
    }
}

Making the class non-instantiable is a good idea where possible (i.e. when an instance of that class is meaningless/useless) because it simplifies the reasoning about the code: you never have to worry about what to do when you encounter an instance of it.

There is an item specifically about this in Effective Java: it's Item 22 in 3rd Ed: "Use interfaces only to define types". TL;DR: if SwingDevHelper isn't a meaningful type, don't use an interface.

答案2

得分: 0

如果提出这个问题,那么这个方法就不属于一个接口(广义上的“接口”,不仅仅是Java)。因此,最好将其放在尽可能低的位置。换句话说,如果可能的话,将其放在一个类中,只将必要的成员移动到接口中。

英文:

If this question is raised then this method does not belong to an interface ('interface' in broad meaning, not just Java). So it is always better to put it as low as possible. In other words, if it is possible -- keep it in a class, and move to interface only essential members.

huangapple
  • 本文由 发表于 2023年2月10日 15:54:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/75408273.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定