I have a Publisher object that raises an event at regular intervals (think of it something like a clock tick). I then have 100s (or 1000s) of subscriber objects (all instances of a single type or two) which may have to update themselves in response to the event. But most of the time the event won't apply to them, so they just ignore it. So the event handling method in the Subscriber looks something like this:
private void changeHandler(int id)
{
if (id == this.ID) // rarely true
val++;
}
I'm concerned because the inefficiency of this approach is starting to take a hit on performance. (Dozens of events raised every second, with thousands of subscribers, is becoming non-negligible.)
Now, it just so happens that the Publisher, in this case, does happen to know which subscribers each particular event applies to (which is how it can pass the id
parameter), so I could bypass the Event strategy altogether and have the Publisher just update each subscriber "manually" (so to speak). But that would make the system more tightly coupled, which I was trying to avoid.
Is there a more efficient approach to this design strategy than I am currently using? Or a better pattern altogether that would maintain separation of concerns between the Publisher and the Subscribers?