Thanks for all the answers. Zaknes came closest to give me the answer I needed, however I came up with a slightly simpler model.
My main goal was to avoid using a static variable in my main data structure. I also ran into the problem of trying to find out if this basic data structure was successfully retrieved from the database during an attempt to access it and what to do when it is absent (i.e. when it is zero).
After watching the Google Web Toolkit Architecture: Best Practices for archiving your GWT application from Google IO, the Event Bus idea seemed perfect.
I will post my solution here if this helps someone else.
First create the Handler class. Pay attention to the link to the Event class:
public interface CategoryChangeHandler extends EventHandler { void onCategoryChange(CategoryChangeEvent event); }
Now to the Event class. This gave me a big nuisance:
public class CategoryChangeEvent extends GwtEvent<CategoryChangeHandler> { private final List<Category> category; public CategoryChangeEvent(List<Category> category) { super(); this.category = category; } public static final Type<CategoryChangeHandler> TYPE = new Type<CategoryChangeHandler>(); @Override protected void dispatch(CategoryChangeHandler handler) { handler.onCategoryChange(this); } @Override public com.google.gwt.event.shared.GwtEvent.Type<CategoryChangeHandler> getAssociatedType() { return TYPE; } public List<Category> getCategories(){ return category; } }
Now I can use these Handler and Event classes, for example, when this basic data structure is reloaded:
This code has received a data structure and wants to notify everyone who listens that it has been updated:
CategoryChangeEvent event = new CategoryChangeEvent(result); eventBus.fireEvent(event);
This code is an implementation of the event.
public class PopulateCategoryHandler implements CategoryChangeHandler { @Override public void onCategoryChange(CategoryChangeEvent event) { tearDownCategories(); List<Category> categories = event.getCategories(); populateCategories(categories); } }
Nick Jun 21 '09 at 3:49 2009-06-21 03:49
source share