Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To answer this question, let me first pose another way of thinking about this same kind of problem - instead of events, we'll just use methods.</p> <p>In my tiered application, two modules communicate via an interface (notice that these methods are all <code>void</code>, so they are rather like events - the caller doesn't expect an answer back):</p> <pre class="lang-java prettyprint-override"><code>package com.acme.project; public interface CompanyServiceInteface { public void addCompany(CompanyDto company) throws AcmeBusinessLogicException; public void updateCompany(CompanyDto company) throws AcmeBusinessLogicException; public void deleteCompany(CompanyDto company) throws AcmeBusinessLogicException; } </code></pre> <p>This seems like overkill to me - why not just reduce the size of this API to one method, and add an enum argument to simplify this. This way, when I build an alternative implementation or need to mock this in my unit tests, I just have one method to build instead of three. This gets to be clearly overkill when I make the rest of my application - why not just <code>ObjectServiceInterface.modify(Object someDto, OperationEnum invocation);</code> to work for all 10 modules?</p> <hr> <p>One answer is that you might want want to drastically modify the implementation of one but not the others - now that you've reduced this to just one method, all of this belongs inside that switch case. Another is that once simplified in this way, the inclination often to further simplify - perhaps to combine <code>create</code> and <code>update</code> into just one method. Once this is done, all callsites must make sure to fulfill all possible details of that method's contract instead of just the one specific one.</p> <p>If the receivers of those events are simple and will remain so, there may be no good reason to not just have a single ModelModifiedEvent that clearly is generic enough for all possible use cases - perhaps just wrapping the ID to request that all client modules refresh their view of that object. If a future use case arises where only one kind of event is important, now the event must change, as must all sites that cause the event to be created so that they properly populate this new field.</p> <p>Java shops typically don't use Java because it is the prettiest language, or because it is the easiest language to write or find developers for, but because it is relatively easy to maintain and refactor. When designing an API, it is important to consider future needs, but also to think about what it will take to modify the current API - your IDE almost certainly has a shortcut key to find all invocations of a particular method or constructor, allowing you to easily find all places where that is used and update them. So consider what other use cases you expect, and how easily the rest of the codebase can be udpated.</p> <p>Finally, don't forget about generics - for my example above, I would probably make a DtoServiceInterface to simplify matters, so that I just declare the one interface with three methods, and implement it and refer to it as needed. In the same way, you can make one set of three <code>GwtEvent</code> types (with <code>*Handler</code> interfaces and possibly <code>Has*Handlers</code> as well), but keep them generic for all possible types. Consider <code>com.google.gwt.event.logical.shared.SelectionEvent&lt;T&gt;</code> as an example here - in your case you would probably want to make the model object type a parameter so that handlers can check which type of event they are dealing with (remember that generics are erased in Java), or source from one <code>EventBus</code> for each model type.</p>
    singulars
    1. This table or related slice is empty.
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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