As I've been told, one should not declare parameters to client GWT
code as interfaces like List<T>. The typical way to write a JavaBean
containing a list is this:

public class Frames {
   private List<Integer> frames = new ArrayList<Integer>();
   public void setFrames(List<Integer> frames) {
    this.frames = frames;
  }
  // ...
}

Google recommends that GWT classes use the specific ArrayList<T>
instead, because these declarations will force GWT to generate code
for LinkedList<T> and every other implementation of List.

However, this runs into another problem: encapsulation.  The class
Frames2 is unsafe:

public class Frames2 {
   private ArrayList<Integer> frames = new ArrayList<Integer>();
   public void setFrames(ArrayList<Integer> frames) {
    this.frames = frames;
  }
 public ArrayList<Integer> getFrames() {
    return frames;
  }
}

It suffers from aliasing, as setting the frames instance variable
allows the setter to interfere with the internals of the Frames2
instance:

Frames2 frames = new Frames2();
ArrayList<Integer> myScoreLine = new ArrayList<Integer>();
myScoreLien.add(9);
frames.setFrames(myScoreLine);
myScoreLine.set(0, 30);  // CHEAT!

Usually, one copies the lists on both setting and getting:

public class Frames3 {
   private ArrayList<Integer> frames = new ArrayList<Integer>();
   public void setFrames(ArrayList<Integer> frames) {
    this.frames = new ArrayList<Integer>(frames);
  }
 public ArrayList<Integer> getFrames() {
    return new ArrayList<Integer>(frames);
  }
}

This solves the aliasing problem, but is slow. Perhaps I should not
vend the list but provide access to its members. I probably should
provide a way of getting all the list's members at once:

public class Frames4 {
   private ArrayList<Integer> frames = new ArrayList<Integer>();
   public void addFrame(int score) {
    frames.add(score);
  }

  public Iterator<Intgeer> frames() {
    return frames.iterator();
  }
}

Will this force GWT to generate code for every Iterator<E> in my
system?  I probably am programming with too much paranoia, of course.






-- 
You received this message because you are subscribed to the Google Groups 
"Google Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to