Constants should not be defined in interfaces
2017-08-17 17:03
423 查看
According to Joshua Bloch, author of "Effective Java":
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.
or
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.
Noncompliant Code Example
interface Status { // Noncompliant int OPEN = 1; int CLOSED = 2; }
Compliant Solution
public enum Status { // Compliant OPEN, CLOSED; }
or
public final class Status { // Compliant public static final int OPEN = 1; public static final int CLOSED = 2; }
相关文章推荐
- new types may not be defined in a return type错误
- new types may not be defined in a return type
- codeblocks中Error:new types may not be defined in a return type错误
- : error: new types may not be defined in a return type
- Strict standards: Non-static method cls_image::gd_version() should not be called statically in
- discards qualifiers || new types may not be defined in a return type|| shadows a parameter
- G++, new types may not be defined in a return type
- webpack打包遇到locals[0] does not appear to be a `module` object with Hot Module replacement API enabled. You should disable react-transform-hmr in production by using `env` section in Babel configuration
- 编译出错"new types may not be defined in a return"
- FAIL - Context /SsmApp is defined in server.xml and may not be undeployed
- 错误记录--Multiple constructor definitions found. Constructor may not be defined in
- You should not be calling AfxGetMainWnd in a worker thread
- 错误记录--Multiple constructor definitions found. Constructor may not be defined in
- - Templates should only be responsible for mapping the state to the UI. Avoid placing tags with side-effects in your templates, such as <script>, as they will not be parsed.
- commit() should not be called while in auto-commit mode
- Non-static method cls_image::gd_version() should not be called statically in D:\X\www\ecshop\install
- ecshop二次开发:Non-static method cls_image::gd_version() should not be called statically in的解决
- 关于C++中报Error:new types may not be defined in a return type问题
- 安装ecmall报错Strict Standards: Non-static method ECMall::startup() should not be called statically in D
- "new types may not be defined in a return type" error