Callback/Command vs EventListener/Observer Pattern

╄→尐↘猪︶ㄣ 提交于 2019-11-28 15:21:28

Both patterns are great and which one to choose depends on what are you going to build and how your framework will be used.

If you are trying to build some kind of publish-subscribe system with following typical flow of work:

  • client starts async task and forgets about it
  • multiple handlers receives notifications when task is completed

then Observer pattern is a natural choice for you. As you are doing a framework you should also consider using EventBus pattern to achieve loose coupling.

If you need nothing more than a simple asynchronous execution and a typical flow using of your framework is:

  • start async task
  • do something when it is completed

or

  • start async task
  • do something
  • wait till it is completed and do something

then you should go with simple Callback.

But in order to achieve more usable and clean API I'd recommend you to get rid of Callback abstraction and design your worker code to return a some kind of Future.

public interface Worker<T> {

    Future<T> doAsync();

}

And Worker can be used following way:

Future<Integer> future = worker.doAsync();

// some work here

Integer result = future.get(); // waits till async work is done

Future could be a standard java Future. But I'd suggest you to use ListenableFuture from guava library.

saintedlama

Command, callback and observer patterns have different semantics:

  • callback - notifies a single caller that some operation finished with some result
  • observer - notifies zero to n interested parties that some event (for example a finished operation) happened
  • command - encapsulates a operation call in an object thus making it transferable over a wire or persist-able

In your example you could combine both callback and observer patterns to achieve greater API flexibility:

  1. Use the callback pattern to trigger operations and notify the caller asynchronously that the triggered operation finished.
  2. Use the event/observer pattern to give some other components (that did not trigger the operation) the chance to be notified when an operation finishes.

I'd argue that the callback pattern is better as it is simpler which means it'll be more predictable and less likely to have bugs due to it's own mutating state. An example of this in operation would be the way GWT handles browser / server communication.

You might want to use generics though:

//example callback
public interface Callback<T> {
    public void notify(T result);
}

//example method
public class Worker{
  public void doAsyncWork(Callback<SomeTypeOrOther> callback){
     //do work
     callback.notify(result);
  }
}

Both patterns share few common intentions except,

Observable pattern can notify multiple listeners. On the other hand, command pattern will be best suited, where you need single callback handler.

In command pattern, it's easy to implement the undo operation.

Cheers!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!