This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark ""Constant Interface" Anti-Pattern" Watch ""Constant Interface" Anti-Pattern" New topic
Author

"Constant Interface" Anti-Pattern

Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4967

So, I'd always heard that the "Constant Interface", that is, creating a crazy multiple-inheritance tree of extended and inherited interfaces, all of which do nothing more than hold constants, was a major anti-pattern. Has the "Constant Interface" come back into Vogue?

I'm working with some GWT code that came right from the labs at Google, and I'm seeing just this crazy maze of constant interfaces that is taking me a while to wrap my head around. Is this becomming more common?

Here's the definition of the ShowcaseConstants interface. Personally, I don't find that this makes reading through and maintaining this code base any easier. Comments?



Constant Interface Anti-Pattern Defined

-Cameron McKenzie


Author of Hibernate Made Easy, What is WebSphere???, JSF 2.0 Made Easy and the SCJA Certification Guides
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24057
    
  13

That is pretty unbelieveable!

When the first edition of Bloch was written, Java didn't have static imports yet, and inheriting an interface like this was therefore the only way to use the unadorned constants. Now you could say

import static ShowcaseConstants.CONSTANT_ONE;
import static ShowcaseConstants.CONSTANT_TWO;
import static ShowcaseConstants.CONSTANT_THREE;

and use only the ones you want, instead of "implementing" the interface. Personally, I think this is absolutely fine, and you might even consider it convenient to have all those constants in one place.

Block says you could put these in uninstantiable final classes instead of interfaces, but then you can't use multiple inheritance to compose them this way.


[Jess in Action][AskingGoodQuestions]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
That's just plain nasty!


Android appsImageJ pluginsJava web charts
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4967

It seems that every class declares its own interface, and then ShowcaseConstants extends each one of these interfaces, so every time you create a new class, you add to the number of interfaces that are extended by ShowcaseConstants.

From ShowcaseConstants, we see some type of reading of a properties file which is internationalized. So, it's all done to keep all Strings out of any Java classes, and I guess enforce through compile time checking that all Strings are indeed referenced from this one place. When the application runs, all of the real values are pulled from the i18n bundles.

What is it called when you start to admire the people that are holding you hostage? Stockholm Syndrome or something?

-Cameron McKenzie
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

there isnt a choice here Cameron Wallace McKenzie,
It isnt pure Java
Its GWT.

http://code.google.com/p/google-web-toolkit/source/browse/releases/1.6/samples/showcase/src/com/google/gwt/sample/showcase/client/ShowcaseConstants.java?spec=svn4598&r=4598


My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

GWT is a framework that allows programmers to code in java but converts that code into javascript. It does not support java 100% as the language is
eg. no Calendar class, no Vectors, etc.
You cant expect it to have a javascript equivalent of everything,
besides If I am not wrong they (Google) are planning to implement GWT in python, etc..
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
salvin francis wrote:GWT is a framework that allows programmers to code in java but converts that code into javascript. It does not support java 100% as the language is
eg. no Calendar class, no Vectors, etc.
You cant expect it to have a javascript equivalent of everything,

What does any of this have to do with the fact that the code exhibits a rather bad practice?
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

Ulf Dittmer wrote:
salvin francis wrote:GWT is a framework that allows programmers to code in java but converts that code into javascript. It does not support java 100% as the language is
eg. no Calendar class, no Vectors, etc.
You cant expect it to have a javascript equivalent of everything,

What does any of this have to do with the fact that the code exhibits a rather bad practice?

The code would have not been like that (At least I wouldnt expect Google to) had it not been GWT.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35247
    
    7
Not sure what you're saying - it's OK because it's GWT? Bad code is bad code, no matter where it originated.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 915

I am not saying it's OK because it's GWT,
I am saying that yes, its bad code,
but there is a reason why they didn't use properties file/ database/any other programming construct.

As mentioned before,

The code would have not been like that had it not been GWT.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: "Constant Interface" Anti-Pattern
 
Similar Threads
Use of Enums over Interface constants ?
Interface
Is this a right way? "Defining an Interface to store constants"
interface constants
Constants