Original Post
Hey everyone, I have to implement something where I feel the Observer pattern fits. There are multiple objects that observe/listen to certain events. At the moment, they listen to one type of event, but in the future, one class may have to listen to more than one type of event. Which sounds better out of these two:
1. Use a common IObservable interface, but make override NotifyObservers() in each subclass... that way I can call different functions on the observers based on the type of event... like "onEventA()" and "onEventB()"...
2. Create a single Observable class with a dictionary that maps each event type to a list of observers. Observers register with that single Observable object. Then, some class may call FireEvent(EventType et) on the single Observable class, which would grab the appropriate list of observers from the dictionary, and then call onEvent(EventType et) on every observer on that list. That means doing a switch(et) inside onEvent().
Thanks!
1. Use a common IObservable interface, but make override NotifyObservers() in each subclass... that way I can call different functions on the observers based on the type of event... like "onEventA()" and "onEventB()"...
2. Create a single Observable class with a dictionary that maps each event type to a list of observers. Observers register with that single Observable object. Then, some class may call FireEvent(EventType et) on the single Observable class, which would grab the appropriate list of observers from the dictionary, and then call onEvent(EventType et) on every observer on that list. That means doing a switch(et) inside onEvent().
Thanks!