Jump to content
  • Advertisement
Sign in to follow this  
m0nkfish

Events

This topic is 3497 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm looking at implementing an object framework and it'll involve events. There are basically two routes here - use a library (boost) or write my own (I've never done it before). This project is purely for my own development and enjoyment, so I want to write my own, even if they won't be as efficient as the boost library. So my approach would be to have a Publisher class that stores a list of function pointers and overloads the += and -= operators (similar to C# .NET) to add a Subscriber function. Is this the right way to go about it?

Share this post


Link to post
Share on other sites
Advertisement
I would suggest that += and -= for subscription is operator abuse. I know Microsoft did it, but they sometimes use shortcuts which don't always set the best example.

Anyway, I believe what you're referring to is the Observer pattern. This is a reasonable approach, generally, but you need to consider whether you need synchronous events or asynchronous events and whether you're intending on having events occur across threads or only within a single thread. Depending on your needs, your approach may be changed a bit.

For example, in single-threaded code, I think the Observer pattern works great because of its simplicity. In multi-threaded code, one thread might have a subscription message and its entirety is sitting in a loop, processing messages. Then when an event occurs, the subscribee sends a message with the event to the subscriber.

There's other mechanisms, as well. There's also slots and signals, which are present in both Boost and Qt and I believe that's what you were referring to when you mentioned Boost.

Share this post


Link to post
Share on other sites
Yeah, having looked up the Observer pattern that is exactly what I wanted :)

For now, my framework will have to be single-threaded, since I don't fully understand how it's all going to sit together yet.

I'll look into slots and signals since I haven't encountered them yet, thanks for the heads-up.

Why do you say that += is operator abuse? Since there are no other possibilities for the += operator, I think it's a nice elegant way of showing the addition and removal of observers...?

Share this post


Link to post
Share on other sites
Quote:
Original post by m0nkfish
Yeah, having looked up the Observer pattern that is exactly what I wanted :)

For now, my framework will have to be single-threaded, since I don't fully understand how it's all going to sit together yet.

I'll look into slots and signals since I haven't encountered them yet, thanks for the heads-up.

Why do you say that += is operator abuse? Since there are no other possibilities for the += operator, I think it's a nice elegant way of showing the addition and removal of observers...?


Well generally + and - are mathematical-related operations. By using them for subscription, it's non-intuitive. It's trying to be too clever, but it actually damages the readability of the code. What if you subscribee also supports addition? You might have code that looks like this:


mathSubscribee += listener;
mathSubscribee += 3;




Double yuck. Most people reading the code won't immediately recognize the first line as the observer pattern. Better make code that is easier to read and has a bit of self-documenting nature. There's definitely various opinions on the matter, but the gurus generally say not to try to be too cute for this sort of thing.

Much easier to read:


mathSubscribee.register(listener);
mathSubscribee += 3;


Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!