Constant interface

Constant interface

In the Java programming language, the constant interface pattern describes the use of an interface 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 is considered inappropriate. [1][2] In general, collecting system constants into classes independent of behaviour, might create a poor object-oriented design, because it is often a sign of low cohesion. It is for these reasons that implementing constants interfaces is considered to be an anti-pattern.

Use of this anti-pattern has a few other downsides:

  1. It pollutes the class namespace with read-only variables that may not be of use.
  2. Contrary to the compile-time tactical utility of implementing a constants interface, the incidental run-time artifacts have little practical purpose (cf. marker interfaces which also have no methods but are useful at run-time).
  3. If binary code compatibility is required in future releases, the constants 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.
  4. Without an IDE that resolves where the constant are coming from, tracking it back to its containing class or interface can be time consuming.
  5. A variable (representing an instance) of the interface is syntactically no more useful than the interface name itself (since it has no methods).

Example

public interface Constants {
 
        public static final double PI = 3.14159;
        public static final double PLANCK_CONSTANT = 6.62606896e-34;
}
 
 
public class Calculations implements Constants {
 
        public double getReducedPlanckConstant() {
                return PLANCK_CONSTANT / (2 * PI);
        }
}


Alternatives

Many of the pitfalls of the anti-pattern can be avoided by converting the constants interface to a proper class with no instances:

public final class Constants {
 
        private Constants() {
                // restrict instantiation
        }
 
        public static final double PI = 3.14159;
        public static final double PLANCK_CONSTANT = 6.62606896e-34;
}

This still leaves the original intent of the pattern mostly un-addressed (i.e. there is no syntax for accessing the constants unqualified). However, since Java 5, consider using static import[2] to achieve the same goal:

import static Constants.PLANCK_CONSTANT;
import static Constants.PI;
 
public class Calculations {
 
        public double getReducedPlanckConstant() {
                return PLANCK_CONSTANT / (2 * PI);
        }
}


To varying degrees, the issues listed above have now been addressed:

  1. Because static members can be imported specifically, the class namespace need not be polluted with all members of the constants interface.
  2. Run-time and compile-time semantics are more closely aligned when using static imports instead of constants interfaces.
  3. The compiled code has one less binary compatibility constraint (that "class Calculations implements Constants").
  4. 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.
  5. There is less need to declare variables of the constants interface type, and it is potentially clearer that no concrete instances actually exist.

Note however, the changes do nothing to improve the cohesion of the Constants class, so static imports should not be considered to be a panacea.

References

  1. ^ Bloch, Joshua, Effective Java, 2nd Edition, p. 98
  2. ^ a b Sun Microsystems, Inc. (2004). "Static Import".

Wikimedia Foundation. 2010.

Игры ⚽ Поможем решить контрольную работу

Look at other dictionaries:

  • Constant (programming) — const redirects here. For the keyword, see const correctness. In computer programming, a constant is an identifier whose associated value cannot typically be altered by the program during its execution (though in some cases this can be… …   Wikipedia

  • Interface (Java) — An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain… …   Wikipedia

  • Interface (computing) — For other uses, see Interface. In the field of computer science, an interface is a tool and concept that refers to a point of interaction between components, and is applicable at the level of both hardware and software. This allows a component,… …   Wikipedia

  • Interface metaphor — An Interface metaphor is a set of user interface visuals, actions and procedures that exploit specific knowledge that users already have of other domains. The purpose of the interface metaphor is to give the user instantaneous knowledge about how …   Wikipedia

  • History of the graphical user interface — The graphical user interface, understood as the use of graphic icons and a pointing device to control a computer, has over the last four decades a steady history of incremental refinements built on some constant core principles. Several vendors… …   Wikipedia

  • Mode (computer interface) — This article is about a transient state in user interfaces. For modes in videogames, see Game mode. For other uses, see Mode (disambiguation). Not to be confused with Multimodal interaction or Modality (human computer interaction). In user… …   Wikipedia

  • The Humane Interface — Infobox Book name = The Humane Interface author = Jef Raskin country = United States language = English genre = Computer Science publisher = Addison Wesley release date = 2000 isbn = ISBN 0 201 37937 6 The Humane Interface: New Directions for… …   Wikipedia

  • POSIX terminal interface — The POSIX terminal interface is the generalized abstraction, comprising both an Application Programming Interface for programs, and a set of behavioural expectations for users of a terminal, as defined by the POSIX standard and the Single Unix… …   Wikipedia

  • Common Gateway Interface — This article is about the interface between a web server and an external application. For the term CGI in computer graphics, see Computer generated imagery. The Common Gateway Interface (CGI) is a standard (see RFC 3875: CGI Version 1.1) method… …   Wikipedia

  • Elastic interface bus — Overview= Elastic Interface buses (EI bus connections) can be generalized as bus connections which are high speed interfaces that have clock sent with data. All of the data bits are aligned to the clock to be able to latch the data at these high… …   Wikipedia

Share the article and excerpts

Direct link
Do a right-click on the link above
and select “Copy Link”