Is there a way to make sure classes implementing an Interface implement static methods?

后端 未结 9 1949
礼貌的吻别
礼貌的吻别 2021-01-07 17:11

First of all, I read erickson\'s useful reply to \"Why can’t I define a static method in a Java interface?\". This question is not about the \"why\" but about the \"how then

9条回答
  •  终归单人心
    2021-01-07 17:54

    Constructor in interface? uh? Would you like to be able to call Interface i = new Interface(double[] parameters)? And the computer would another time choose himself the implementation? This is as much strange as static in interface :D

    As you said, checking parameters should be done before construction... But this doesn't mean you can't raise exception on construction if parameters are not ok. It's just a security you can add, that will ensure that the constructed object will be coherent. But such code doesn't allow you to bypass a previous validation: raising exception on construction will tell you "hey you have a bug!" while not validating the params will just tell you "hoho someone using GUI tried to set a bad value, we'll send him an error message..."

    Actually, since you need to validate the values, and the object is not even constructed, why do you absolutly want to add this validation on the model object? Form/Gui/whatever validation could be done anywhere, in a validation java class... Just set a static (or not) method, in another class called ParametricFunctionValidationHelper, where you add your method and the validation implementation.

    public static boolean validateParametricFunction(String functionType, double[] parameters) {
      if ( functionType.equals("bessel") ) return validateBessel(parameters);
      if ( functionType.equals("parabola") ) return validateParabola(parameters);
    }
    

    It doesn't matter how is represented your functionType (i choose String because i suppose you get it from user interface, web or gui... it could have been Enum...

    You can even validate the object after having constructed it:

    public static boolean validateParametricFunction(ParametricFunction pamFunc) {
      if ( pamFunc instanceOf BesselFunction ) return validateBessel(pamFunc.getParameters);
      ......
    }
    

    You can even put static validation methods in function classes and then you'll have: public static boolean validateParametricFunction(ParametricFunction pamFunc) { if ( pamFunc instanceOf BesselFunction ) return BesselFunction.validateBessel(pamFunc.getParameters); if ( pamFunc instanceOf ParabolaFunction ) return ParabolaFunction.validateParabola(pamFunc.getParameters); }

    Yes you won't be able to set the static method in the interface but anyway how would you call such a method?

    With code like

    public static boolean validateParametricFunction(ParametricFunction pamFunc) {
      return ParametricFunction.validate(pamFunc);
    }
    

    ??? This as no sense because the JVM won't be able at all to know which implementation of the static method to use since you don't call the static method from an instance but from a class! It as only sense if you implement the validate method directly in the ParametricFunction class, but anyway if you do such a thing you'll have to do exactly the same that i've shown you before with the instanceOf, because the instance of the pamFunc is the only item you'll have to select which kind of validation you'll have to use...

    That's why you'd better use a non static method and put it in the interface like:

    public static boolean validateParametricFunction(ParametricFunction pamFunc) {
      return pamFunc.validate();
    }
    

    Actually what you should do is: - Retrieve parameters (String?) from GUI / Web interface / anything - Parse String parameters in good format (String to int...) - Validate these parameters with a validation class (static method or not) - If no validation -> print message to user - Else construct object - Use the object

    I don't see anywhere a need of static method in interface...

提交回复
热议问题