I am unable to understand the meaning of Has****Handlers
interfaces in GWT. What would be the difference if a class implements HasClickHandlers
(addClickHandler
) and ClickHandler
(onClick
) interfaces.
thank you
HasClickHandlers
- something that can be clicked, e.g. a button
ClickHandler
- some code that handles on a click
A HasClickHandlers
object is a widget, like a button, that can react when the user clicks on it. But a button by itself does not know what should happen when a user clicks on it. A developer can craft a ClickHandler
object, which is some code that implements what should happen when the user clicks on that button. A button can be given a ClickHandler
to react to the user's click, i.e. the button can have/hold a click handler - HasClickHandlers
.
One may ask why does GWT say applications should define view interfaces with method signatures like:
HasClickHandlers getSaveButton();
instead of simply
Button getSaveButton();
Google advocates decoupling the view from presenter. The presenter usually cares very little for all the functionality of a button - it usually only cares that the button is something that can take a click handler and use it. An interface like HasClickHandler
has very few methods and is very easy to mock. Using a real button however will sometimes require initializing some or part of the whole UI framework and instantiating prerequisite context classes in order to create a button for testing.
By having the interface return HasClickHandler
instead Button
, the unit test code for the presenter can decouple completely from the complexity of the UI framework by simply mocking interfaces like HasClickHandler
. This means simpler test scaffolding and very fast unit tests (since you don't have the overhead of initializing/interacting with a UI framework).
http://googletesting.blogspot.com/2009/08/tott-testing-gwt-without-gwttest.html
Edit
OP asks:
ok, e.g. if ABC class implements Hasclickhandlers and Clickhandler and then onClick and addClickHandler (which returns HandlerRegistration), it means that 1)it will act on click event thru onClick method and 2)will let any other class(es) know (who is implementing ClickHandler and used addClickHandler of ABC class to register the event) that click has just been occurred? right?
Your classes like ABC
will not implement HasClickHandlers
. Only GWT widgets like buttons implement HasClickHandlers
. Google is simply providing the HasClickHandlers
interface as an alternative way to declare variable references to some widgets like buttons. These widgets will notify registered ClickHandler
about a button click.
Your class ABC
may implement ClickHandler
or may contain an inner (possible anonymous) class that derives from ClickHandler
. A typical usage looks like:
public class ABC {
...
getSaveButton().addClickHandler(
new ClickHandler() {
public void onClick(ClickEvent event) {
saveToDatabase();
}
}
}
...
The HasClickHandlers is for objects that generate click events. The ClickHandler is for objects that deal with the events.
For example, a Button will generate a click event. When you want to handle a click event, you create a ClickHandler that contains the code that does so. You register this ClickHandler with the Button object so that when a click happens, the Button knows who to tell about it. The HasClickHandlers interface is implemented by Button (via the FocusWidget parent class) which is just the interface for registering ClickHandlers. This interface simply standardizes the registering of ClickHandlers.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With