tags:

views:

68

answers:

0

Hey there! I'm relatively new to both GWT and java programming (or OOP for that matter), so apologies for the beginner questions/mistakes in advance. I've been trying to create some kind of observer pattern, but the development mode console keeps dropping error messages and sadly, they're far from helpful.

So here's what I'm trying to achieve: - I've got the model that consists of the class Country, and stores a value called Influence. - The view is the class called CountryDisplay. It's a GWT widget that should always display the current influence of a given country.

public class Country {
   private int influece;
   private CountryDisplay display;

   public Country() {
      influence = 0;
   }
   public void setDisplay(CountryDisplay display) //...
   public int getInfluence() //...
   public void setInfluence(int value) {
      influence = value;
      display.update();
   }
}
public class CountryDisplay {

   private Country country;

   public CountryDisplay (Country country) {
      //GWT widget creating stuff
      this.country = country;
   }
   public void update() {
      //InfluenceCounter is a simple Label
      InfluenceCounter.setText(Integer.toString(country.getInfluence()));
   }
}

Then in the EntryPoint class I do something like this:

Country italy = new Country(); 
CountryDisplay italyDisplay = new CountryDisplay(italy);
italy.setDisplay(italyDisplay);
RootPanel.get("nameFieldContainer").add(italyDisplay);
italy.setInfluence(3);

The development console indicated that it had a problem with the line "display.update();" in class Country. My first guess was that the problem was that the display was not initiated, so I created an interface for it, and in the Country constructor I created an empty, new display, that would later be overwritten.

   public Country() {
      influence = 0;
      display = new DisplayInterface() {
          public void update() {}
      }
   }  

But I had no luck this way either. I guess this kind of cross-referencing is not allowed? I mean that the view has the model as a variable and vice versa. When calling a method on the view individually (like:

italy.setInfluence(3);
italyDisplay.displayTheCurrentValue();

) it works, so the problem is definitely in the observer logic.