aspose file tools*
The moose likes Beginning Java and the fly likes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark ""Constant Interface Antipattern" ?" Watch ""Constant Interface Antipattern" ?" New topic
Author

"Constant Interface Antipattern" ?

Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Hey, can somebody please elaborate to me what "Constant Interface Antipattern" mean. It wasn't much clear to me reading about it in java documentation.
Thanks
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
An Anti-Pattern (generally speaking) is a design approach that has several drawbacks.

As for the Constant Interface Anti-Pattern, check here http://en.wikipedia.org/wiki/Constant_interface


JDBCSupport - An easy to use, light-weight JDBC framework -
John Bengler
Ranch Hand

Joined: Feb 12, 2009
Posts: 133
Hi Deepak,

the Constant Interface Antipattern is about defining constants in interfaces and then let the classes, in which these constants are used, implement these interfaces.
It is a kind of workaround for some people because in Java a Class can't inherit from multiple classes (but it can implement multiplei nterfaces)

If you do a search for this you should find plenty of threads on the net, e.g.:

Constants Interface Antipattern

The problem is that you add stuff to the interface of this class which doesn't belong there.

Some say you should completely avoid to use interfaces to define constants, because you provoke people to use them in the Antipattern way.
You can find an alternative aproach in the link above.

Hope this made it a little clearer to you...


John
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Little clearer now.....thanks
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Deepak,

You can create an interface like this:


The reason why this is a bad idea is that the interface, instead of specifying a contract for behavior (methods), just passes some not too meaningful constants around. You're much better off creating a class.

John.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39843
    
  28
The real problem about implementing John's interface is that all classes implementing it now expose public A_CONSTANT_INT and A_CONSTANT_STRING fields for the whole world to see, and once these classes have been used by other classes, those fields can't be changed for fear of "breaking" those unsuspecting "other classes".
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18907
    
    8

John de Michele wrote:You're much better off creating a class.


Yes. Or these days you might consider creating an Enum for a group of related constants.
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45
Thanks guys.
Lesson learnt:

1) Don't create interfaces just for using constants globally.
2) Use a private public class instead, class with a private constructor and static constants, and then statically import them.

I hope I got this right.

Thanks again.This is great forum for beginners like me.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39843
    
  28
Why not use a public class (with a private constructor) to encapsulate your public static final variables?
Deepak Borania
Ranch Hand

Joined: Jul 28, 2009
Posts: 45

That was what I actually meant. . .
Shaily Sharma
Ranch Hand

Joined: Sep 10, 2007
Posts: 39
hi all,
there is a nice way to use constants defined in a class to use it in another class.(applicable for java1.5 onwards).
here it goes:

public class Constants {
//Constants for other classes
static final String HOST_NAME = "host_name";
.
.
}

import static Constants.*;
class UseMyConstants {
String hostname;

init(){
hostname = HOST_NAME;
}

}

all d best...
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14348
    
  22

See this: Java 5 New Language Features - Enums

One of the reasons for adding enums to Java is to avoid the constant interface anti-pattern.

In prior releases, the standard way to represent an enumerated type was the int Enum pattern:

This pattern has many problems, such as:

  • Not typesafe - Since a season is just an int you can pass in any other int value where a season is required, or add two seasons together (which makes no sense).
  • No namespace - You must prefix constants of an int enum with a string (in this case SEASON_) to avoid collisions with other int enum types.
  • Brittleness - Because int enums are compile-time constants, they are compiled into clients that use them. If a new constant is added between two existing constants or the order is changed, clients must be recompiled. If they are not, they will still run, but their behavior will be undefined.
  • Printed values are uninformative - Because they are just ints, if you print one out all you get is a number, which tells you nothing about what it represents, or even what type it is.



  • Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: "Constant Interface Antipattern" ?