Observer pattern question
#1 Members - Reputation: 352
Posted 25 June 2012 - 03:19 PM
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!
#2 Members - Reputation: 503
Posted 26 June 2012 - 02:14 AM
#3 Members - Reputation: 1613
Posted 26 June 2012 - 02:05 PM
class IObserverInterface1
{
virtual void HandleThisEvent();
};
class IObserverInterface2
{
virtual void HandleThatEvent();
}
// This class gets notified via the calls
class ObservingClass : public IObserverInterface1, public IObserverInterface2
{
void HandleThisEvent();
void HandleThatEvent();
}
// in .cpp file
// register to listen for the events I want
ClassForThisEvent->Register(this);
ClassForThatEvent->Register(this);
void ObservingClass::HandleThisEvent()
{
}
void ObservingClass::HandleThatEvent()
{
}
Edited by BeerNutts, 26 June 2012 - 02:05 PM.
---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)
#4 Members - Reputation: 352
Posted 26 June 2012 - 03:29 PM
What about having different interfaces for each class being observed? Something like this:
I forgot to mention I'm working with C# (it doesn't support multiple inheritance)... however, under different circumstances, the approach is a good one, so thanks
Edited by gfxgangsta, 26 June 2012 - 03:30 PM.
#5 Members - Reputation: 214
Posted 26 June 2012 - 04:02 PM
What about having different interfaces for each class being observed? Something like this:
I forgot to mention I'm working with C# (it doesn't support multiple inheritance)... however, under different circumstances, the approach is a good one, so thanks
But you can still implement multiple interfaces. The equivalent to BeerNutts code in C# would be something like
interface IObserverInterface1
{
void HandleThisEvent();
}
interface IObserverInterface2
{
void HandleThatEvent();
}
public class ObservingClass : IObserverInterface1, IObserverInterface2
{
public void HandleThisEvent()
{
// Handle this event
}
public void HandleThatEvent()
{
// Handle that event
}
}
#6 Members - Reputation: 1528
Posted 26 June 2012 - 05:46 PM
class Event
{
}
class FireEvent : public Event
{
// bla bla bla
}
class IEventObserver
{
public void OnEventTriggered(Event e);
}
class FireEventObserver : public IEventObserver
{
public void OnEventTriggered(Event e)
{
if (e is FireEvent)
{
// do something
}
}
}
#7 Members - Reputation: 690
Posted 26 June 2012 - 06:31 PM
class FireEventObserver : public IEventObserver { public void OnEventTriggered(Event e) { if (e is FireEvent) { // do something } } }
Promotes control coupling, no good.
http://en.wikipedia....er_programming)
interface IObserverInterface1
{ void HandleThisEvent(); }
interface IObserverInterface2
{ void HandleThatEvent(); }
public class ObservingClass : IObserverInterface1, IObserverInterface2
{
public void HandleThisEvent()
{
// Handle this event
}
public void HandleThatEvent()
{
// Handle that event
}
}
In my opinion this is over engineering in this particular case, but my be perfect. (You didn't say what your application is exactly) Also this is good if you want to strongly
type different kinds of observers for whatever reason.
Since you are using C#, I would suggest you just use delegates with the dictionary idea if you need multiple observers. Plus if you use events, all of the code for setting and removing observers to/from subjects is generated in the compilation process (+= and -= for events are the things I'm talking about, it's just syntax fluff).
Edited by M6dEEp, 26 June 2012 - 06:33 PM.






