• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

"Constant Interface" Anti-Pattern

 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's just plain nasty!
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
salvin francis
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1268
10
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic