简体   繁体   English

Java-返回通用基本类型的Factory方法

[英]Java - Factory Method that returns generic Base type

I'm trying to genericize a factory method that returns a generic Base class. 我正在尝试泛化返回通用基类的工厂方法。 It works, but I'm getting the "BaseClass is a raw type..." warning. 它可以工作,但是我收到“ BaseClass is a raw type ...”警告。

I've read through the Java docs on Generic methods, but I'm still not quite getting how to accomplish this. 我已经阅读了有关泛型方法的Java文档,但是我仍然不太了解如何完成此操作。

Here's some code: 这是一些代码:

Class #1 1级

//base abstract class
public abstract class BaseFormatter<T>
{
    public abstract String formatValue(T value);
}

Class #2 2级

//two implementations of concrete classes
public class FooFormatter extends BaseFormatter<Integer>
{
    @Override
    public String formatValue(Integer value)
    {
        //return a formatted String
    }
}

Class #3 3级

public class BarFormatter extends BaseFormatter<String>
{
    @Override
    public String formatValue(String value)
    {
        //return a formatted String
    }
}

Factory Method in a separate class 单独类中的工厂方法

public static BaseFormatter getFormatter(Integer unrelatedInteger)
{
    if (FOO_FORMATTER.equals(unrelatedInteger))
        return new FooFormatter();
    else if (BAR_FORMATTER.equals(unrelatedInteger))
        return new BarFormatter();
    //else...
}

Call to the Factory Method from elsewhere in the code 从代码中的其他地方调用Factory方法

BaseFormatter<Integer> formatter = getFormatter(someInteger);
formatter.formatValue(myIntegerToFormat);

The problem is the getFormatter() method warns that BaseFormatter is a raw type, which it is. 问题是getFormatter()方法警告BaseFormatter是原始类型。 I've tried various things like BaseFormatter et al. 我已经尝试了诸如BaseFormatter等其他各种方法。 I, of course, want the return type to be generic, as in the declared BaseFormatter in the calling method. 我当然希望返回类型是通用的,就像调用方法中声明的BaseFormatter一样。

Note that the formatter type is not based on class type. 请注意,格式化程序类型不是基于类类型的。 eg not all Integer values are formatted with a FooFormatter. 例如,并非所有Integer值都使用FooFormatter格式化。 There are two or three different ways an Integer (or String, or List) can be formatted. 整数(或字符串或列表)的格式有两种或三种不同的格式。 That's what the param unrelatedInteger is for. 这就是参数unrelatedInteger的目的。

Thanks in advance for any feedback. 预先感谢您的任何反馈。

If getFormatter is defined in BaseFormatter, then use: 如果在BaseFormatter中定义了getFormatter,则使用:

public static BaseFormatter<T> getFormatter(Integer unrelatedInteger)

If getFormatter is defined in another class than BaseFormatter, then use: 如果getFormatter是在BaseFormatter之外的另一个类中定义的,则使用:

public static BaseFormatter<?> getFormatter(Integer unrelatedInteger)

You're actuaaly saying that there's no connection between the typed parameter of BaseFormatter and the unrelatedInteger that is passed as argument to the getFormatter method. 您实际上是在说BaseFormatter的类型化参数unrelatedInteger作为参数传递给getFormatter方法的BaseFormatter之间没有任何联系。

I get some other warning: 我收到其他警告:

Uncehcked Assignment: BaseFormatter to BaseFormatter<Integer>

This warning is worse than the one you indicated. 此警告比您指出的警告要严重。 It warns that this user code might try to insert a BaseFormatter<String> into BaseFormatter<Integer> , something that will be noticed only when fails in runtime... Consider a user accidentally uses you factory method like such: 它警告该用户代码可能会尝试将BaseFormatter<String>插入BaseFormatter<Integer> ,这种情况只有在运行时失败时才会出现……考虑用户不小心使用了您的工厂方法,例如:

BaseFormatter<Integer> myUnsafeFormatter =
        FormatterFactory.getFormatter(unrelatedIntegerForBarFormatter);

The compiler cannot relate the unrelatedInteger with the parameterized type of the returned BaseFormatter . 编译器不能将BaseFormatter与返回的BaseFormatter的参数化类型unrelatedInteger BaseFormatter

Alternitavely, I'd let the user explicitly use the concrete formatter constructors. 或者,我让用户明确使用具体的格式化程序构造函数。 Any common code shared by all formatters could be put into FormatterUtils class (just don't let that utils class to grow to much...). 所有格式化程序共享的任何通用代码都可以放入FormatterUtils类中(只是不要让该utils类增长太多...)。

Some type systems in academic languages can express a so-called dependent sum. 学术语言中的某些类型系统可以表示所谓的从属和。 Java certainly cannot; Java当然不能; so what, sensibly, could be the type of the object returned by the getFormatter method? 那么明智的是,getFormatter方法返回的对象的类型是什么? The best we can do is BaseFormatter< ? extends Object > 我们能做的最好的就是BaseFormatter< ? extends Object > BaseFormatter< ? extends Object > , or BaseFormatter< ? > BaseFormatter< ? extends Object >BaseFormatter< ? > BaseFormatter< ? > for short, as Integer and String have only Object in common. BaseFormatter< ? >简而言之,因为IntegerString仅具有相同的Object

I think the original post begs the question, why must we use an integer to decide what formatter to return, and if the type of formatter would not be known by the caller, why would the caller need a stronger variable type than BaseFormatter< ? > 我认为原始帖子提出了一个问题,为什么我们必须使用整数来决定要返回的格式化程序,并且如果调用方不知道格式化程序的类型,为什么调用方需要比BaseFormatter< ? >更强的变量类型BaseFormatter< ? > BaseFormatter< ? > ? BaseFormatter< ? >

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM