【问题标题】:Create a static generic factory in java?在java中创建一个静态泛型工厂?
【发布时间】:2014-02-27 16:13:32
【问题描述】:

我在使用getInstance 方法时遇到了问题,我尝试了任何方法:

Cannot make a static reference to the non-static type T.

我怎样才能做到这一点?任何代码审查也会有很大的帮助。 我检查了几个相关的帖子,但这似乎是一个特殊的案例。

我的代码:

public class Data<Template>
{
    private Template[] data;
    private int id;

    public Data(Template[] data)
    {
        this.data = data;
        id = Arrays.hashCode(data);
        log();
    }

    public Template[] getData()
    {
        return data;
    }

    public Integer getId()
    {
        return id;
    }

    private void log()
    {
        System.out.println(this.toString());
    }

    @Override
    public String toString()
    {
        StringBuilder tString = new StringBuilder();
        tString.append(id + ":");
        for (int i = 0; i < data.length; i++)
        {
            tString.append(data[i] + " ");
        }
        return tString.toString();
    }
}

public class Store<T>
{
    private Queue<Data<T>> queue;
    private ConcurrentHashMap<Integer, Data<T>> inProgress;
    private List<T> results;
    private static Store instance = null;

    private Store()
    {
        queue = new ConcurrentLinkedQueue<Data<T>>();
        inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
        results = new ArrayList<>();
    }

    public void addToStore(Data<T> elment)
    {
        queue.offer(elment);
    }

    public Data<T> getNextTask()
    {
        Data<T> element = queue.poll();
        inProgress.put(element.getId(), element);
        return element;
    }

    public void taskFinishedSuccessfully(Integer id, T result)
    {
        inProgress.remove(id);
        results.add(result);
    }

    public List<T> getResults()
    {
        return results;
    }

    public static Store getInstance(Class<T> type)
    {
        if (instance == null)
        {
            if (type instanceof Integer)
            {
                instance = new Store<Integer>();
            }

            if (type instanceof Float)
            {
                instance = new Store<Float>();
            }

            if (type instanceof Double)
            {
                instance = new Store<Double>();
            }
        }
        return instance;
    }
}

【问题讨论】:

  • 不清楚您在这里尝试做什么,或者它如何可能正常工作/做任何有用的事情。
  • @BrianRoach 好吧,这个想法是您在大型 csv 文件中读取,每一行都是一个任务(一个数字数组),当有人开始处理该任务时,这些任务被放入队列中因为正在进行中,但同时其他人也可以获得队列的任务。
  • 我的意思是试图制作一个可以以某种方式更改类型的单例位。如果您已经创建并尝试返回 Store&lt;Float&gt;,您认为如何调用 getInstance(Integer.class)
  • 你说得对,我没想到我只是关注所有客户都应该使用同一家商店的事实。我仍然不清楚如何实现这一点。但谢谢你花时间回答@BrianRoach

标签: java generics factory


【解决方案1】:

所以这就是我最终做的:

public class Store<T> {
private Queue<Data<T>> queue;
private ConcurrentHashMap<Integer, Data<T>> inProgress;
private List<T> results;
private static Store<Double> doubleInstance = null;
private static Store<Integer> integerInstance = null;
private static Store<Float> floatInstance = null;

private Store() {
    queue = new ConcurrentLinkedQueue<Data<T>>();
    inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
    results = new ArrayList<>();
}

public void addToStore(Data<T> elment) {
    queue.offer(elment);
}

public Data<T> getNextTask() {
    Data<T> element = queue.poll();
    inProgress.put(element.getId(), element);
    return element;
}

public void taskFinishedSuccessfully(Integer id, T result) {
    inProgress.remove(id);
    results.add(result);
}

public List<T> getResults() {
    return results;
}

private static <T> Store<T> getInstance() {
    return new Store<T>();
}

public static Store<Double> getDoubleInstance() {
    if (doubleInstance == null) {

        doubleInstance = Store.<Double> getInstance();
    }
    return doubleInstance;
}

public static Store<Integer> getIntegerInstance() {
    if (integerInstance == null) {

        integerInstance = Store.<Integer> getInstance();
    }
    return integerInstance;
}

public static Store<Float> getFloatInstance() {
    if (floatInstance == null) {

        floatInstance = Store.<Float> getInstance();
    }
    return floatInstance;
}

}

【讨论】:

    【解决方案2】:

    不清楚你为什么不只是写作

    public static <T> Store<T> getInstance() {
      return new Store<T>();
    }
    

    【讨论】:

    • 对不起我的无知,但我只是不知道,如果我会这样写,我将如何给出类型参数?像 Store s=Store.getInstance() ?
    • 认为 OP 正试图使其成为单例。这是我唯一能想到的。
    • 是的,最初的想法是,让它成为单身
    • @IszlaiLehel:在这个版本中,它会自动为你推断出来。如果需要明确,可以写Store.&lt;Double&gt;getInstance()
    • 如果您遇到问题non-static type variable T cannot be referenced from a static context,您应该输入参数化getInstance 方法,@LouisWasserman 已向您展示了方法,但我认为这不足以让编译器满意
    猜你喜欢
    • 1970-01-01
    • 2011-01-19
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-16
    • 1970-01-01
    相关资源
    最近更新 更多