Alternatives to static methods on interfaces for enforcing consistency

前端 未结 7 2355
孤城傲影
孤城傲影 2020-12-05 02:43

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

7条回答
  •  再見小時候
    2020-12-05 03:13

    A totally different approach (and an ugly hack, for that matter) is to let the interface have a method that returns a method.

    public interface MyInterface{
        Method getConvertMethod();
    }
    

    now your client code can do

    yourInterface.getConvertMethod().invoke(objectToBeConverted);
    

    This is extremely powerful, but very bad API design

    Sean

提交回复
热议问题