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 Listcallbacks = 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.