File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
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: 4968
    
    1

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
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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: 42585
    
  65
That's just plain nasty!


Ping & DNS - my free Android networking tools app
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper

Joined: Aug 26, 2006
Posts: 4968
    
    1

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: 928

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: 928

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: 42585
    
  65
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: 928

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: 42585
    
  65
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: 928

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://aspose.com/file-tools
 
subject: "Constant Interface" Anti-Pattern