Wednesday, November 16, 2011

Is this a valid use of an interface?

I saw a piece of code that made me wonder if this was normal when using interfaces or if it is just abusing the functionality of an interface... The code looks like so:
 
public interface Constants {
    public static final String URL = "localhost:1099";
    ... // Other constants defined
}

Later on it is used like so:
 
public class Something implements Constants {
     ...
     Context ctx = new InitialContext( PropertiesFactory.getProperties( URL ) );
     ...
}

Is  it normal practice to create an interface with a bunch of constants in an interface (without any methods defined), then implement the interface in a class and use the constants? 

I would have just statically accessed those parameters directly rather than doing it this way. If you have an opinion, answer to my thought or are just bored, let me know.  Well, perhaps not if you're bored.

No comments: