I am trying to develop a system that will be based on processing certain events and generating data. Each event will contain (possibly) several different fields, and each listener will process some or all of them. I have two approaches:
In the event generation class, I will register several event listeners, each of which listens for one specific value of a specific event field, for example:
public class MicroListener implements Listener { public void processEvent(Event e){ if(e.getName().equals(registeredName)) { ... } }
This is tempting because the processing is performed inside the object itself, and there is no centralized event processing, which allows each object to process information. The drawback, possibly fatal, is the fact that each event (out of several hundred thousand) should be transmitted to all listeners, while only a small fraction will really do it. This will probably lead to a great effective result in the long run ...
A centralized listener that will listen and act on all events and delegate processing to the relevant event processors, for example:
public class CentralListener implements Listener { Map<String, Processor> processorsByName; public void processEvent(Event e){ processorsByName.get(e.getName()).process(e); } }
It will be faster, but for any other parts of the event, separate cards or processor collections, for example, will be required. a processor that checks for an event identifier, etc. This does not apply to approach 1. Because we simply generate a different set of listeners and register them with the event generation class.
What do you guys think about this? Do they make sense, or would you rather advise completely differently?
source share