Thursday, June 26, 2014

Re: Why not use class name as the TYPE of the event

Hi

Use the qualified class name would disallow to extend events sharing the same EventHandler.
On the bellow code you can handle all the AbstractSessionEvent extentions with the single AbstractSessionEvent.Handler interface.

public abstract class AbstractSessionEvent extends Event<Handler> {
    public interface Handler extends EventHandler {...}

protected static Type< Handler > TYPE = new Type< Handler >();
@Override
protected void dispatch(Handler handler){ … }
}

public class LoginEvent extends AbstractSessionEvent{
public Event.Type< Handler > getAssociatedType() {
return TYPE;
}
}

public class LogoutEvent extends AbstractSessionEvent{
public Event.Type< Handler > getAssociatedType() {
return TYPE;
}
}


Their is probably mush more reasons. But I think this one is good enough.

Regards

--
Fabien.


Le 26 juin 2014 à 09:00, smiletolead <kumar.ganesha@gmail.com> a écrit :

Hi all,

A custom event needs to have a TYPE variable to denote the type of the event. For example:

public class LogoutEvent extends Event<LogoutEventHandler>
{
public static Type<LogoutEventHandler> TYPE = new Type<LogoutEventHandler>();
@Override
protected void dispatch(LogoutEventHandler handler)
{
handler.onLogout();
}

@Override
public com.google.web.bindery.event.shared.Event.Type<LogoutEventHandler> getAssociatedType()
{
return TYPE;
}

}


I am just curious about the need to have the TYPE field. Why GWT could not use the class name of the event(in this case LogoutEvent) to denote the type of the event. Only purpose I see of this type is to have it as a key in the map of events and their handlers. I think even the class name could have served as the key in this map.

Thanks
Ganesh


No comments:

Post a Comment