First, you will useFunctional interfaceConsumer, this can decouple the callback method, and first write an abstract listening interface
Listenable
import .*; import ; public interface Listenable<Listener> { WeakHashMap<Object, Set> LISTENERS_WEAK_MAP = new WeakHashMap<>(); default void registerListener(Listener listener) { (listener); Set<Listener> listeners; synchronized (LISTENERS_WEAK_MAP) { listeners = LISTENERS_WEAK_MAP.get(this); if (listeners == null) { listeners = new HashSet<>(); LISTENERS_WEAK_MAP.put(this, listeners); } } synchronized (listeners) { (listener); } } default void unregisterListener(Listener listener) { (listener); Set<Listener> listeners; synchronized (LISTENERS_WEAK_MAP) { listeners = LISTENERS_WEAK_MAP.get(this); if (listeners == null) { return; } } synchronized (listeners) { (listener); } } default Collection<Listener> getListeners() { synchronized (LISTENERS_WEAK_MAP) { Set<Listener> listeners = LISTENERS_WEAK_MAP.get(this); if (listeners == null) { return new HashSet<>(); } return new HashSet<>(listeners); } } default boolean isListenerRegistered(Listener listener) { synchronized (LISTENERS_WEAK_MAP) { Set<Listener> listeners = LISTENERS_WEAK_MAP.get(this); if (listeners == null) { return false; } return (listener); } } default void forEachListener(Consumer<Listener> action) { forEachListener(action, true); } default void forEachListener(Consumer<Listener> action, boolean ignoreException) { (action); Iterable<Listener> listeners; synchronized (LISTENERS_WEAK_MAP) { Set<Listener> values = LISTENERS_WEAK_MAP.get(this); if (values == null) { return; } listeners = new ArrayList<>(values); } for (Listener listener : listeners) { try { (listener); } catch (Exception e) { if (!ignoreException) { throw e; } } } } }
Practical usage
The usage is also relatively simple, in your own implementation classMessageManager In, write a custom callbackOnEventListener, and then implement the notification method, so that you can easily write various different types of callbacks that require one-to-many notifications.
public class Main { public static class MessageManager implements Listenable<> { private MessageManager() {} private static final MessageManager instance = new MessageManager(); public static MessageManager getInstance() { return instance; } public void onSuccess(String json) { forEachListener(it->(json)); } public void onError(int code, String error) { forEachListener(it->(code, error)); } public interface OnEventListener { void onSuccess(String json); void onError(int code, String error); } } public static void main(String[] args) { ().registerListener(new () { @Override public void onSuccess(String json) { ("onSuccess " + json); } @Override public void onError(int code, String error) { ("onError code:" + code + " error:" + error); } }); ().onSuccess("My json"); ().onError(-1, "Error"); } }
Print results
onSuccess My json
onError code:-1 error:Error
This is the end of this article about implementing callback listening tools based on Java. For more related Java callback listening content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!