工厂对象创建者与泛型

时间:2013-06-02 20:28:00

标签: java generics factory-pattern

我想在java中使用泛型做一个工厂模式。我的代码是:

界面:

public abstract class Factory<T> {
    public abstract T create();
}

FactoryA类:

public class FactoryA extends Factory<FactoryA> {

    public FactoryA() {

    }

    public FactoryA create() {
        return new FactoryA();
    }

}

FactoryB类:

public class FactoryB extends Factory<FactoryB> {

    public FactoryB() {

    }

    public FactoryB create() {
        return new FactoryB();
    }

}

主要班级:

public class FactoryCreator {

    public static <T> T createFactory() {
        Factory<T> t = ?; // is that right way?
        return t.create();
    }

    public static void main(String[] args) {
        FactoryA factoryA = FactoryCreator.createFactory();
        FactoryB factoryB = FactoryCreator.createFactory();
    }
}

问题是,工厂t = 需要相同,还是有其他方法?

2 个答案:

答案 0 :(得分:1)

不确定你想要达到的目标,但这可能会有所帮助;

public interface Factory<T> 
{
    public T create(String type);
    public T create(String type, Object arg);
    public T create(String type, Object[] args);
}

然后有一个类工具,工厂界面,像这样;

public class TemplateFactory<T> implements Factory {

    @Override
    public T create(String type) throws IllegalArgumentException
    {
        return create(type, null);
    }

    @Override
    public T create(String type, Object arg) throws IllegalArgumentException
    {
        // Convert to array of 1 element
        Object[] arguments = new Object[1];
        arguments[0] = arg;
        return create(type, arguments);
    }

    @Override
    public T create(String type, Object[] args) throws IllegalArgumentException
    {
        // Create array for all the parameters
        Class<?> params[] = (args != null) ? new Class<?>[args.length] : new Class<?>[0];
        if(args != null)
        {
            // Adding the types of the arguments
            for(int i = 0; i < args.length; ++i)
                params[i] = (args[i] != null) ? args[i].getClass() : null;
        }

        try
        {
            // Create a class variable
            Class classLoader = Class.forName(type);
            // Find the right constructor
            Constructor co;
            if(params.length > 0)
                co = classLoader.getConstructor(params);
            else
                co = classLoader.getConstructor();
            // Instantiate the class with the given arguments
            T newObject = (T)co.newInstance(args);
            return newObject;
        }
        catch(Exception e)
        {
            throw new IllegalArgumentException(e.toString());
        }
    }
}

然后像这样使用它(使用一些想象的策略类作为例子):

TemplateFactory<StrategyInterface> factory;
factory = new TemplateFactory<>();

factory.create("packageName.StrategyA");
factory.create("packageName.StrategyB");
factory.create("packageName.StrategyC");

策略类(A,B和C)将在此示例中实现StrategyInterface类。

答案 1 :(得分:0)

这样的事可能有用:

public static <T extends Factory> T createFactory(Class<T> clazz) {
    try {
        t = clazz.newInstance();
        return t.create();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}
...
FactoryA factoryA = FactoryCreator.createFactory(FactoryA.class);

或者,没有参数。但是你需要两种方法。

public static FactoryA createFactoryA() {
   return new FactoryA().create();
}
...
FactoryA factoryA = FactoryCreator.createFactoryA();

由于Generic类型在运行时被擦除,因此您必须提供Class参数,以便运行时知道您正在讨论的是哪个类。