跳至主要内容

Anti-pattern of Constant Interface in Java

There is a famous book named "Effective Java", whose author is Joshua Bloch. In this book, there is a chapter about the "constant interface" anti-pattern. Besides, a new grammar named "static import" has been added into the language since Java 5.0. As explained in Sun's document, this feature is designed to encourage developers to use utility class rather than constant interface.

Quote:
In order to get around this, people sometimes put static members into an interface and inherit from that interface. This is a bad idea. In fact, it's such a bad idea that there's a name for it: the Constant Interface Antipattern (see Effective Java Item 17). The problem is that a class's use of the static members of another class is a mere implementation detail. When a class implements an interface, it becomes part of the class's public API. Implementation details should not leak into public APIs.

评论