HOME       >>       Programming

Constant Interface Or Constant Class?


longer

I always use classes to hold constants instead of interfaces. Recently my co-worker started using Interface to hold constants for the same project.I think it's good to share with him some points of view from the following books which justify why should you avoid 'Constant Interface':Philip Heller and Simon Roberts, "Complete Java 2 Certification StudyGuide":"In the first place, to say that a class implements an interface really means that the class exposes the public methods listed in theinterface. Interfaces are for defining types and should be used exclusively for that purpose. Constant interfaces only contain data, so they definitely don?t define types.The second disadvantage is a bit more complicated. Suppose someone you work with writes some code that uses an instance of Scales. This person can legally reference that instance with a variable of type Conversion, even though doing so would be quite inappropriate. Later, if you wanted to eliminate the Conversion interface, you couldn?t do so, because your misguided colleague would be relying on the existence of the interface."Joshua Bloch, "Effective Java - Programming Language Guide":"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."Although it's like preference for different people, Interface is for defining types, and not solely for holding constants.Further more, we can use import static in Java 5 so we can type as less as using interface. Finally he accepts the changes and move to his code from interface to class:)


dimumurray

If an interface is used purely as a holder for constants then there won't be much of an issue. I think problems crop up when faced with the option of being able to implement the interface as well. You can treat the 'Constant Interface' as you would any Class with static members defined on them by just using the interface name and referencing the desired constant using 'dot' form like so:

[interface_name].[constant_name]

 

You just have to resist the temptation to implement the interface. Static imports should also apply to interfaces with constants so you're pretty much good to go either way. I can see your point though. 'Constant Interfaces' don't adhere to the ways in which you would traditionally use interfaces, but that is sometimes the nature of a design pattern. They sometimes eschew conventional practice in favor of achieving desired functionality. There are always trade offs.

 

Have you considered using enums? They are more bloated than say a class with constant static members but they provide a less dubious format for your constants.



VIEW DESKTOP VERSION REGISTERGET FREE HOSTING

Xisto.com offers Free Web Hosting to its Members for their participation in this Community. We moderate all content posted here but we cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our terms of use, cookie and privacy policy. Copyright 2001-2019 by Xisto Corporation. All Rights Reserved.