java - 在 Web 项目中定义一组常量的最佳方法是什么?

标签 java

我正在开发一个 ejb 项目,我需要定义一组可以在整个项目中使用的常量,因此我创建了一个接口(interface)来保存我的常量集,我的问题是有更好的方法吗在Web项目中定义常量?

这是我的常量界面的示例:

public interface ejbConstant {
    final String  CONST23= "AB23";
    final String  CONST24= "AB24";
    // ..........
}

感谢您的建议

最佳答案

Sonar已经相当不错了explanation of this with examples

根据《Effective Java》一书的作者 Joshua Bloch 的说法:

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. It is of no consequence to the users of a class that the class implements a constant interface. In fact, it may even confuse them. Worse, it represents a commitment: if in a future release the class is modified so that it no longer needs to use the constants, it still must implement the interface to ensure binary compatibility. If a nonfinal class implements a constant interface, all of its subclasses will have their namespaces polluted by the constants in the interface.

关于java - 在 Web 项目中定义一组常量的最佳方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40659222/

相关文章:

java - 外部 java 库(jars)实际上是如何工作的?

java - 在 S3 存储桶策略中添加自定义条件

java - java字符串的效率

java - 如何获取动态创建的文本字段的值

java - minify-maven-plugin cssFinalFile 在哪里?

java - iText:在 CellEvent 期间设置单元格高度

java - instanceof 运算符的使用

JAVA - 无法编译

java - 不能从静态上下文中引用非静态方法 toString()

java - 如何使用 Maven 运行特定包中的所有测试?