简体   繁体   English

类中的静态最终字段与 Java 中的接口字段

[英]Static final field in a class vs Interface field in java

I need to create a 100 or more static final constants in my application and I can achieve this is following two ways as per my understanding:我需要在我的应用程序中创建 100 个或更多静态最终常量,根据我的理解,我可以通过以下两种方式实现:

  1. Creating a simple java class and create static final field in that创建一个简单的 java class并在其中创建static final字段
  2. Creating an interface an put all variable in that because all field in an interface is implicitly static final创建一个接口并将所有变量放入其中,因为接口中的所有字段都是隐式static final

I have these question in above approach:我在上述方法中有这些问题:

  1. Which one is right approach to achieve this?哪一种是实现这一目标的正确方法?
  2. Which one is memory efficient approach?哪一种是内存高效的方法?
  3. Is there any design pattern to achieve this?是否有任何设计模式可以实现这一目标?

You can refer to many books about the topic.您可以参考许多有关该主题的书籍。

I will quote a good one: "Effective Java"我会引用一个很好的:“Effective Java”

Item 19: Use interfaces only to define types第 19 条:仅使用接口来定义类型

The constant interface pattern is a poor use of interfaces.常量接口模式是接口使用不当。 That a class uses some constants internally is an implementation detail.一个类在内部使用一些常量是一个实现细节。 Implementing a constant interface causes this implementation detail to leak into the class's exported API.实现一个常量接口会导致这个实现细节泄漏到类的导出 API 中。 It is of no consequence to the users of a class that the class implements a constant interface类实现常量接口对类的用户没有任何影响

you can even check where JDK mostly constants are declared..您甚至可以检查 JDK 中大多数常量的声明位置。

Math.PI for example is declared in the class Math and not in an interface例如Math.PI在类 Math 中声明,而不是在接口中

and as an exception you can see constants like in the java.io.ObjectStreamConstants but again the Books are there to help:作为一个例外,您可以在java.io.ObjectStreamConstants 中看到类似的常量,但这些书籍同样可以提供帮助:

From effective java again:再次从有效的java:

There are several constant interfaces in the Java platform libraries... Java 平台库中有几个常量接口......

These interfaces should be regarded as anomalies and should not be emulated.这些接口应被视为异常,不应进行仿真。

I would not be thinking should they be in an interface or class, but more about the constants and their meaning.我不会考虑它们是否应该在接口或类中,而是更多关于常量及其含义。

I would not recommend putting all your constants in one place for the sake of keeping them together.我不建议为了将它们放在一起而将所有常量放在一个地方。 If for instance a constant is directly related to a class then would say put it in that class.例如,如果一个常量与一个类直接相关,那么就会说把它放在那个类中。 I have worked with code where all the constants ate bundled into one class, and I don't thing it is a good approach.我曾经使用过将所有常量捆绑到一个类中的代码,我认为这不是一个好方法。

Have you considered approach with ENUM or it doesn't fit in your case?您是否考虑过使用 ENUM 的方法或它不适合您的情况? I think, the approach with ENUM can gives you some benefits over constants.我认为,使用 ENUM 的方法可以为您提供一些优于常量的好处。

Why use Enums instead of Constants? 为什么使用枚举而不是常量?

I think that convenient way is to keep them in one place, if they are have common nature.我认为方便的方法是将它们放在一个地方,如果它们具有共同的性质。 Anyway, they should be grouped by some attribute.无论如何,它们应该按某种属性分组。 You can create class for them like this:您可以像这样为他们创建类:

public final class Consts {
    public static class GroupA {...}
    public static class GroupB {...}
    //and so on
}

With groups this class becomes much readable and a little bit better manageable.通过组,这个类变得更具可读性,并且更易于管理。 About memory consumption, try to use primitives for your constants, because they do not require additional space for meta information.关于内存消耗,尝试为常量使用原语,因为它们不需要额外的元信息空间。

您可以通过在接口类中声明字段来随意创建最终或静态约束,因此我想使用您的选项编号 2

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

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