Java - 强制子类具有静态方法的替代方法 [英] Java - Alternatives to forcing subclass to have a static method

查看:106
本文介绍了Java - 强制子类具有静态方法的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常发现我想做这样的事情:

I often find I want to do something like this:

class Foo{
public static abstract String getParam();
}

强制Foo的子类返回参数。

To force a subclasses of Foo to return a parameter.

我知道你不能这样做我知道为什么你不能这样做但是常见的选择:

I know you can't do it and I know why you can't do it but the common alternative of:

class Foo{
public abstract String getParam();
}

不满意是因为它要求你有一个实例,如果你没有帮助只是想知道参数的值并且实例化这个类是很昂贵的。

Is unsatisfactory because it requires you to have an instance which is not helpful if you just want to know the value of the parameter and instantiating the class is expensive.

我很想知道人们怎么解决这个问题而不打算使用恒定界面反模式。

I'd be very interested to know of how people get around this without getting into using the "Constant Interface" anti pattern.

编辑:我会添加一些关于我的具体问题的更多细节,但这只是我想要做这样的事情的当前时间还有其他几个来自过去。

I'll add some more detail about my specific problem, but this is just the current time when I've wanted to do something like this there are several others from the past.

我的子类是所有数据处理器,超类定义它们之间的公共代码,允许它们获取数据,解析数据并将其放在需要的地方去。
每个处理器都需要在SQL数据库中保存的某些参数。每个处理器应该能够提供它所需的参数列表和默认值,以便通过检查每个处理器类型所需的参数来验证配置数据库或将其初始化为默认值。
在处理器的构造函数中执行它是不可接受的,因为每个类只需要每个对象实例执行一次,并且当每个类的类的实例可能还没有时,应该在系统启动时完成需要。

My subclasses are all data processors and the superclass defines the common code between them which allows them to get the data, parse it and put it where it needs to go. The processors each require certain parameters which are held in an SQL database. Each processor should be able to provide a list of parameters that it requires and the default values so the configuration database can be validated or initialised to defaults by checking the required parameters for each processor type. Having it performed in the constructor of the processor is not acceptable because it only needs to be done once per class not once per object instance and should be done at system startup when an instance of each type of class may not yet be needed.

推荐答案

在静态环境中你可以做的最好的事情就像下面之一:

The best you can do here in a static context is something like one of the following:

a。有一个你专门寻找的方法,但不是任何合同的一部分(因此你不能强制执行任何人)并在运行时寻找:

a. Have a method you specifically look for, but is not part of any contract (and therefore you can't enforce anyone to implement) and look for that at runtime:

 public static String getParam() { ... };
 try {
     Method m = clazz.getDeclaredMethod("getParam");
     String param = (String) m.invoke(null);
 }
 catch (NoSuchMethodException e) {
   // handle this error
 }

b。使用注释,它会遇到同样的问题,因为你不能强迫人们把它放在他们的课堂上。

b. Use an annotation, which suffers from the same issue in that you can't force people to put it on their classes.

@Target({TYPE})
@Retention(RUNTIME)
public @interface Param {
   String value() default "";
}

@Param("foo")
public class MyClass { ... }


public static String getParam(Class<?> clazz) {
   if (clazz.isAnnotationPresent(Param.class)) {
      return clazz.getAnnotation(Param.class).value();
   }
   else {
      // what to do if there is no annotation
   }
}

这篇关于Java - 强制子类具有静态方法的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆