Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a callback instead of returning state object
    primarykey
    data
    text
    <p>In my application, I'm executing a transaction which can be in three different states at the end of the execution:</p> <ul> <li>Success</li> <li>Failure</li> <li>Pending</li> </ul> <p>Depending on the state, the application client will want to perform different actions. In the case of success, they will want to retrieve the transaction result (a Widget). In the case of failure, they will want to receive the error reason. In the case that the transaction is pending, they will want to schedule a time to retry. Currently I return an object with the following methods:</p> <pre><code>public interface Result() { State getState(); Widget getWidget(); // Success Reason getFailureReason(); // Failure Callable&lt;Result&gt; getTask(); // Pending } </code></pre> <p>The idea is that the client checks the state of the result object, and invokes the appropriate method depending on it's value, e.g.</p> <pre><code>if (result.getState() == State.PENDING) { result.getTask(); } </code></pre> <p>I was thinking that it might be preferable to use a callback instead, e.g.</p> <pre><code>public interface TransactionCallback() { void onFailure(Reason reason); void onSuccess(Widget widget); Delay onPending(Delay previous); } </code></pre> <p>Where <code>Delay</code> is a class representing a <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/TimeUnit.html" rel="nofollow noreferrer">TimeUnit</a> and period, allowing the application to reschedule the transaction execution. Another alternative is to throw an exception wrapping the failure reason (as it should only fail in exceptional conditions), and keep the <code>onSuccess</code> and <code>onPending</code> methods.</p> <p>So my question to SO is this: is the use of a callback an appropriate pattern for this particular problem, or can anyone suggest something more appropriate?</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