Question

In my application, I'm executing a transaction which can be in three different states at the end of the execution:

  • Success
  • Failure
  • Pending

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:

public interface Result() {
     State getState();
     Widget getWidget(); // Success
     Reason getFailureReason(); // Failure
     Callable<Result> getTask(); // Pending
}

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.

if (result.getState() == State.PENDING) {
    result.getTask();
}

I was thinking that it might be preferable to use a callback instead, e.g.

public interface TransactionCallback() {
    void onFailure(Reason reason);
    void onSuccess(Widget widget);
    Delay onPending(Delay previous);
}

Where Delay is a class representing a TimeUnit 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 onSuccess and onPending methods.

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?

Was it helpful?

Solution

I don't think this is a good use of the callback pattern.

A callback is appropriate if the callee (e.g. the transaction in your case) needs to continue doing things after a callback method returns. But if the next thing that the callee always does is return to the caller, then the callback doesn't add any value. It is just makes the code structure more complex and less readable. IMO, it would be better to return a result object or (in the case of an exceptional failure) throw an exception.

EDIT - re the OP's comment.

I can see the value of using a callback method to ask the caller if the transaction should continue, though I probably would have used a simple timeout parameter. However, using callback methods to return results is (IMO) still wrong.

OTHER TIPS

I prefer callbacks since if you use it in several places it will result in less code and be more readable. With call back your if statement to determine what is going to happen (which method to call) will be in the service doing the transaction and all the code that uses the service will just look cleaner.

The callback arguments should include the object sourcing the call. Sounds fine for me, but using a callback could involve some synchronization issues - you can't be sure in which state you will receive the callback. Not impossible, but may need consideration.

Of course with polling the result, you would need synchronization on the other side. But that sounds simpler to synchronize.

Callback solution is much extendable, while you may at one point want to change the behavior to an asynchronous call.

The drawback is your code will be less readable, especially to those beginner programmer. If that's not a problem for you then sure, go on...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top