• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Implementing interface with no methods

 
Ranch Hand
Posts: 260
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the benifit of defining an interface with only constants but no methods and implementing the same interface in other classes?
I have seen this style of design in many open source applications.
Why cant the contants in the interface be just referenced where ever required instead of implementing the interface?
Thanks in advance
William
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, the reason people do it is to save typing; that's it. If you implement the interface, you get to type the names of the constants as unqualified names.

This is called the "constant interface pattern." Folks who write about such things have decided this is bad. Joshua Bloch's "Effective Java" makes a detailed case against it.

I personally remain unconvinced. I've been told this means I've never worked on a sufficiently large project to see the problems emerge, but I've worked on million-line codebases that use this pattern and still haven't seen any problems due to it.

In any case, Java 1.5's new "static imports" feature lets you import constants explicitly into a translation unit without re-exporting them as if you had implemented a constant interface.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In practice it's not bad. According to OO theory it's abhorrent because interfaces define contracts between classes and constants are not contracts but rather implementation details.
So it's more a theoretical than a practical antipattern.
 
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It all depends on your background and point of view. If you've ever had to support code written by graduates who though this was a good thing you too would stear clear.

My specific case is two-fold, created by people over using constant interfaces and having a poor understanding of inheritence, and what we ended up with was a complicated interface/object heirarchy mess where it was significant effort to find the definition of one of these constants.

You could argue that experienced programmers wouldn't create this mess, but if it isn't really required why go down that road, and why promote it?
 
Ranch Hand
Posts: 224
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What about you using an interface with constants (no methods) and using it in other classes (only import, no extending).

I used to create a class with private constructor and all the constants and use this in other clases.

Which one is better?
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jeroen Wenting:
In practice it's not bad. According to OO theory it's abhorrent because interfaces define contracts between classes and constants are not contracts but rather implementation details.
So it's more a theoretical than a practical antipattern.



I think it becomes an antipattern when it's overused or used in the wrong context. For example I wouldn't do that for classes which are part of a published interface, simply it would expose clients of that class to its implementation details.

When used with care, I don't see any problems, though. On the other hand, with Tiger the need to do that really vanishes.
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sreenivasa Majji:
What about you using an interface with constants (no methods) and using it in other classes (only import, no extending).

I used to create a class with private constructor and all the constants and use this in other clases.

Which one is better?



I don't see any significant difference, but I might be missing something.
 
Amateurs built google. Professionals built the titanic. We can't find the guy that built this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic