vendredi 17 juillet 2015

Java: Instantiating array object of type T using a factory

I would like to instantiate an array of type T as such:

items = new T[maxQue];

Here is my code so far, I believe a non-reflective approach:

interface MyFactory<T>
{
   T[] newObject();
}

public class Queue< T extends Comparable<T> > {

   private int front;
   private int rear;
   private T[] items;
   private int maxQue;
   private static final int MAX_ITEMS = 1000;

   public Queue(MyFactory<T> factory) {
      maxQue = MAX_ITEMS + 1;
      front = maxQue - 1;
      rear = maxQue - 1;

      items = factory.newObject();
      items = new T[maxQue];
   }
}

The { items = factory.newObject(); } works and resolves the compiler error but I do not know how to set the size of the array to maxQue using the MyFactory interface.

  • How can I declare this array with a size of maxQue?

On a side note, while I know the definition of reflection in Java, can anyone please put it and the concept of factories in layman terms?





Aucun commentaire:

Enregistrer un commentaire