Note that there are some explanatory texts on larger screens.

plurals
  1. POSeemingly redundant event and event handlers
    primarykey
    data
    text
    <p>I will explain with an example. My GWT project has a Company module, which lets a user add, edit, delete, select and list companies.</p> <p>Of these, the add, edit and delete operations lands back the user on the CompanyList page. Thus, having three different events - <code>CompanyAddedEvent, CompanyUpdatedEvent and CompanyDeletedEvent</code>, and their respective event handlers - seems overkill to me, as there is absolutely not difference in their function.</p> <p>Is it OK to let a single event manage the three operations? One alternative I think is to use some event like <code>CompanyListInvokedEvent</code>. However, somewhere I think its not appropriate, is the event actually is not the list being invoked, but a company being added/updated/deleted.</p> <p>If it had been only a single module, I would have get the task done with three separate events. But other 10 such modules are facing this dilemma. It means 10x3 = 30 event classes along with their 30 respective handlers. The number is large enough for me to reconsider. What would be a good solution to this?</p> <p><strong>UPDATE -</strong></p> <p>@ColinAlworth's answer made me realize that I could easily use Generics instead of my stupid solution. The following code represents an event EntityUpdatedEvent, which would be raised whenever an entity is updated.</p> <p>Event handler class -</p> <pre><code>public class EntityUpdatedEvent&lt;T&gt; extends GwtEvent&lt;EntityUpdatedEventHandler&lt;T&gt;&gt;{ private Type&lt;EntityUpdatedEventHandler&lt;T&gt;&gt; type; private final String statusMessage; public EntityUpdatedEvent(Type&lt;EntityUpdatedEventHandler&lt;T&gt;&gt; type, String statusMessage) { this.statusMessage = statusMessage; this.type = type; } public String getStatusMessage() { return this.statusMessage; } @Override public com.google.gwt.event.shared.GwtEvent.Type&lt;EntityUpdatedEventHandler&lt;T&gt;&gt; getAssociatedType() { return this.type; } @Override protected void dispatch(EntityUpdatedEventHandler&lt;T&gt; handler) { handler.onEventRaised(this); } } </code></pre> <p>Event handler interface -</p> <pre><code>public interface EntityUpdatedEventHandler&lt;T&gt; extends EventHandler { void onEventRaised(EntityUpdatedEvent&lt;T&gt; event); } </code></pre> <p>Adding the handler to event bus - </p> <pre><code>eventBus.addHandler(CompanyEventHandlerTypes.CompanyUpdated, new EntityUpdatedEventHandler&lt;Company&gt;() { @Override public void onEventRaised(EntityUpdatedEvent&lt;Company&gt; event) { History.newItem(CompanyToken.CompanyList.name()); Presenter presenter = new CompanyListPresenter(serviceBundle, eventBus, new CompanyListView(), event.getStatusMessage()); presenter.go(container); } }); </code></pre> <p>Likewise, I have two other Added and Deleted generic events, thus eliminating entire redundancy from my event-related codebase.</p> <p>Are there any suggestions on this solution?</p> <p>P.S. > <a href="http://www.mail-archive.com/google-web-toolkit-contributors@googlegroups.com/msg11692.html" rel="nofollow">This discussion</a> provides more insight on this problem.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload