gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:44:21 27 4
gpt4 key购买 nike

我正在开发一个 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com