HOME

TheInfoList



OR:

In the
Java programming language Java is a high-level, class-based, object-oriented programming language that is designed to have as few implementation dependencies as possible. It is a general-purpose programming language intended to let programmers ''write once, run anywh ...
, the constant interface
pattern A pattern is a regularity in the world, in human-made design, or in abstract ideas. As such, the elements of a pattern repeat in a predictable manner. A geometric pattern is a kind of pattern formed of geometric shapes and typically repeated l ...
describes the use of an
interface Interface or interfacing may refer to: Academic journals * ''Interface'' (journal), by the Electrochemical Society * ''Interface, Journal of Applied Linguistics'', now merged with ''ITL International Journal of Applied Linguistics'' * '' Inte ...
solely to define constants, and having classes implement that interface in order to achieve convenient syntactic access to those constants. However, since constants are very often merely an implementation detail, and the interfaces implemented by a class are part of its exported API, this practice amounts to putting implementations details into the API, which was considered inappropriate by, e.g., Java designer
Joshua Bloch Joshua J. Bloch (born August 28, 1961) is an American software engineer and a technology author, formerly employed at Sun Microsystems and Google. He led the design and implementation of numerous Java platform features, including the Java Collec ...
. In general, collecting system constants into classes independent of behaviour might create a poor
object-oriented Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pro ...
design because it is often a sign of low cohesion. For these reasons, constant interfaces may be considered an
anti-pattern An anti-pattern in software engineering, project management, and business processes is a common response to a recurring problem that is usually ineffective and risks being highly counterproductive. The term, coined in 1995 by computer programmer An ...
. Use of this pattern has a few other downsides: # It pollutes the class
namespace In computing, a namespace is a set of signs (''names'') that are used to identify and refer to objects of various kinds. A namespace ensures that all of a given set of objects have unique names so that they can be easily identified. Namespaces ...
with read-only variables that may not be of use. # Contrary to the
compile-time In computer science, compile time (or compile-time) describes the time window during which a computer program is compiled. The term is used as an adjective to describe concepts related to the context of program compilation, as opposed to concept ...
tactical utility of implementing a constant interface, the incidental run-time artifacts have little practical purpose (cf. marker interfaces which also have no methods but are useful at run-time). # If
binary code compatibility Binary-code compatibility (binary compatible or object-code-compatible) is a property of a computer system, meaning that it can run the same executable code, typically machine code for a general-purpose computer CPU, that another computer syste ...
is required in future releases, the constant interface must remain forever an interface (it cannot be converted into a class), even though it has not been used as an interface in the conventional sense. # Without an IDE that resolves where the constant are coming from, tracking it back to its containing class or interface can be time consuming. # An instance of the interface is syntactically no more useful than the interface name itself (since it has no methods). # Unless a developer checks any implemented interfaces when adding a constant to a class, or does so but makes a typo in the name of the added constant, the value of a constant can be silently changed. Consider Example 2 below. Note that the Java libraries use constant interface pattern themselves, showing that it may be a reasonable choice in some situations.


Example 1

public interface Constants public class Calculations implements Constants


Example 2

public interface Constants public class Class1 implements Constants Before the line marked with an asterisk is added, running Class1 prints 1. After adding the line, Class1 prints 2. Both versions compile without warnings or errors.


Alternatives

Many of the pitfalls of the anti-pattern can be avoided by converting the constant interface to a class with static attributes: public final class Constants Since
Java 5 The Java language has undergone several changes since JDK 1.0 as well as numerous additions of classes and packages to the standard library. Since J2SE 1.4, the evolution of the Java language has been governed by the Java Community P ...
, one can use ''
static import Static import is a feature introduced in the Java programming language that allows members (fields and methods) which have been scoped within their container class as public static, to be used in Java code without specifying the class in which the ...
''"Static Import"
/ref> to be able to use the constants without the Constants qualifier: import static Constants.PLANCK_CONSTANT; import static Constants.PI; public class Calculations The constants can also be imported en masse by using an import static Constants.* statement. This achieves the same goals as using an interface, allowing the constants to be referenced without a qualifier. To varying degrees, the issues listed above have now been addressed: # Because static members can be imported specifically, the class namespace need not be polluted with all members of the constant interface. # Run-time and compile-time semantics are more closely aligned when using static imports instead of constant interfaces. # The compiled code has one fewer binary compatibility constraint (that "class Calculations implements Constants"). # Because static imports apply only to the current file (and not the whole class hierarchy) it is easier to discover where each static member is declared. # There is less need to declare variables of the constant interface type, and it is potentially clearer that no concrete instance actually exists. Note, however, that the changes do nothing to improve the cohesion of the Constants class nor prevent the accidental silent modification of the value of a constant, so static imports should not be considered to be a panacea.


References

{{Reflist Java (programming language) Anti-patterns