Alternatives to static methods on interfaces for enforcing consistency

Posted by jayshao on Stack Overflow See other posts from Stack Overflow or by jayshao
Published on 2010-05-10T20:03:13Z Indexed on 2010/05/10 20:24 UTC
Read the original article Hit count: 205

In Java, I'd like to be able to define marker interfaces, that forced implementations to provide static methods. For example, for simple text-serialization/deserialization I'd like to be able to define an interface that looked something like this:

public interface TextTransformable<T>{

  public static T fromText(String text);

  public String toText();

Since interfaces in Java can't contain static methods though (as noted in a number of other posts/threads: here, here, and here this code doesn't work.

What I'm looking for however is some reasonable paradigm to express the same intent, namely symmetric methods, one of which is static, and enforced by the compiler. Right now the best we can come up with is some kind of static factory object or generic factory, neither of which is really satisfactory.

Note: in our case our primary use-case is we have many, many "value-object" types - enums, or other objects that have a limited number of values, typically carry no state beyond their value, and which we parse/de-parse thousands of time a second, so actually do care about reusing instances (like Float, Integer, etc.) and its impact on memory consumption/g.c.

Any thoughts?

© Stack Overflow or respective owner

Related posts about java

Related posts about interfaces