Jump to content

  • Log In with Google      Sign In   
  • Create Account


Observer pattern question


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 gfxgangsta   Members   -  Reputation: 557

Like
0Likes
Like

Posted 25 June 2012 - 03:19 PM

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!

Sponsor:

#2 SabreMan   Members   -  Reputation: 504

Like
1Likes
Like

Posted 26 June 2012 - 02:14 AM

As usual, it depends. There's several factors you may want to think about, such as whether you want to pass arguments with an event, if you want your observer framework to be fully typesafe, whether it needs to be threadsafe, etc. If you're using Boost and you're not so bothered about your own implementation of observer, then boost::signals2 is worth a look.

#3 BeerNutts   Crossbones+   -  Reputation: 2780

Like
1Likes
Like

Posted 26 June 2012 - 02:05 PM

What about having different interfaces for each class being observed? Something like this:
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.

My Gamedev Journal: 2D Game Making, the Easy Way

---(Old Blog, still has good info): 2dGameMaking
-----
"No one ever posts on that message board; it's too crowded." - Yoga Berra (sorta)

#4 gfxgangsta   Members   -  Reputation: 557

Like
0Likes
Like

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 fae   Members   -  Reputation: 214

Like
2Likes
Like

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 Posted Image


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 alnite   Crossbones+   -  Reputation: 2067

Like
1Likes
Like

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 M6dEEp   Members   -  Reputation: 890

Like
2Likes
Like

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.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS