Sunday, October 26, 2014

Invoking callbacks for multiple subscribers

How often do you need to invoke callbacks for a multiple subscribers? For example when you have few observer-like classes.

Consider foolowing code:

public void foo() {
  // do something
  for (Callback callback : mCallbacks) {
    callback.bar();
  }
  // do something
}

Personally, I hate to write code like this - too many lines of code that is mostly duplicate from other places. Even worst when you need to ensure UI thread execution and few more lines go to Handler...

So what is the solution? Most universal (and probably most elegant as well) solution that I've came up with is to use Reflections or to use Proxy to be more specific. Most people I ask didn't even know about existance of this class. In short it allows us to create a class in runtime which implements any number of interfaces. Yes, this adds some overhead but, frankly, this is quite small overhead if used somewhat sparingly and almost non-existantant when we add Handler.

So here is my callbacks invoker implementation:

public class Invoker<T> {

  public final T object;
  public final List callbacks = new ArrayList<>();

  private Invoker(T object) {
    this.object = object;
  }

  @SuppressWarnings("unchecked")
  public static <T> Invoker create(Class cls) {
    DirectInvocation<T> handler = new DirectInvocation<T>();
    Object object = Proxy.newProxyInstance(
        cls.getClassLoader(), new Class[] { cls }, handler
    );
    return handler.invoker = new Invoker<T>((T) object);
  }

  static class DirectInvocation<T> implements InvocationHandler {
    public Invoker<T> invoker;

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
      for (Object object : invoker.callbacks.toArray()) {
        method.invoke(object, args);
      }
      return null;
    }
  }
}

And now some example how to use it:

...
Invoker<Callback> mInvoker = Invoker.create(Callback.class);
...
mInvoker.callbacks.add(...);
mInvoker.callbacks.add(...);
mInvoker.callbacks.add(...);
...

public void foo() {
  // do something
  mInvoker.object.bar(); // invokes all callbacks
  // do something
}

Now it looks much cleaner, In addition we can easily add Handlers, Threads or anything else to invoke out callbacks inside invoker itelf.

3 comments:

  1. Inline 2 of your example, some compilers are less tolerant.
    If that is the case use:
    Invoker mInvoker = Invoker.create(Callback.class);

    ReplyDelete
    Replies
    1. I mean Invoker<Runnable> mInvoker = Invoker.<Runnable>create(Runnable.class);

      Delete
    2. It could be a case for some compiler but I didn't experience this problem yet

      Delete