常量接口最优雅的替代方案是什么?

发布于 2024-12-05 05:36:58 字数 376 浏览 0 评论 0原文

我一直在研究一个离岸团队开发的一些代码。我看到每个模块至少定义了一个“常量接口”。 示例(不是现实世界):

public interface RequestConstants{
  //a mix of different constants(int,string,...)
  public static final int MAX_REQUESTS = 9999;
  public static final String SAMPLE_REQUEST = "Sample Request";
}

根据我的理解,这是一种反模式,因为它们在运行时没有任何用处,应该以不同的方式避免或解决。 有哪些优雅的方式来表示这一点?可以使用枚举来代替吗?

I had been looking at some code developed by an off-shore group. I see at least one "constant interface" per module defined.
Example (not real world) :

public interface RequestConstants{
  //a mix of different constants(int,string,...)
  public static final int MAX_REQUESTS = 9999;
  public static final String SAMPLE_REQUEST = "Sample Request";
}

Per my understanding it is an anti-pattern as these does not any utility in run-time, and should be avoided or tackled in a different way.
What are elegant ways to represent this? Can enums be used instead?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦纸 2024-12-12 05:36:58

我更喜欢将常量放在它们最相关的类中,然后如果我必须在其他地方引用它们,就这样做 - 可能使用静态导入如果这是有道理的(例如对于Math.PI)。

将常量放入接口中的唯一真正原因是允许您“实现”无方法接口并通过常量的简单名称访问常量,而无需任何进一步的限定。静态导入消除了这个原因。

I prefer to put constants in the class where they make they're most relevant, and then if I have to refer to them elsewhere, just do so - possibly using static imports if that makes sense (e.g. for Math.PI).

The only real reason to put constants in interfaces was to allow you to "implement" the method-free interface and get access to the constants via their simple names without any further qualification. Static imports remove that reason.

梦幻的心爱 2024-12-12 05:36:58

除非所有参数都密切相关,否则枚举可能不是一个好主意。对于您示例中的两个参数,我想说它们的相关性不够密切,不足以符合枚举的资格。

但包含这样的常量类/接口并不一定是一个坏主意。它确实具有集中化的优点,这意味着可以轻松地将这些配置内容移至程序之外——例如移至属性文件、命令行解码器、数据库甚至套接字接口——对系统的影响最小。其他班级。这实际上是设计将采取什么方向的问题。

然而,除非您正在考虑走这条路,否则我想说,在使用相应参数的类中使用静态final是正确的方法,正如已经建议的那样。

En enum is probably not a good idea unless all the parameters are closely related. With the two parameters in your example I'd say they are not closely enough related to qualify as an enum.

But it's not necessarily a Bad Idea to include a constants class / interface like this. It does have the advantage of being centralized, which means this configuration stuff can easily be moved outside of the program -- for instance to a properties file, a command-line decoder, a database or even a socket interface -- with minimal impact to the other classes. It's really a question of what direction the design will take.

Unless you are thinking of going down that path, however, I'd say static finals in the classes where the respective parameters are used is the way to go, as has been suggested already.

使用 private 构造函数将接口转换为 final 类。

Turn the interface into a final class with a private constructor.

怀念你的温柔 2024-12-12 05:36:58

使用最终的不可实例化类,即具有私有构造函数的类。

Use final non-instantiable class, i.e. one with a private constructor.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文