Publish-subscribe pattern

Get answers to all your basic programming questions. No Ogre questions, please!
Lektet
Gnoblar
Posts: 21
Joined: Sun May 01, 2011 5:30 pm
x 2

Publish-subscribe pattern

Post by Lektet »

Hello.
I want to use publish/subscribe pattern to implement communication between game engine parts. But the problem is that I cannot find any articles about how it must be implemented in games. Can you, please, give me some useful hints and links on this topic :)
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Publish-subscribe pattern

Post by Klaim »

It's because it's not called that way.

The abstract pattern is called the Observer pattern. There are tons of ways to implement it and it's always relative to several properties you want it to provide.
It's also often called "event system" or event something.

You will find tons of examples and explorative implementations (in C++ at least) by using "observer pattern" in your google research.
User avatar
skyforger
Halfling
Posts: 49
Joined: Tue Apr 13, 2010 10:23 am
Location: Transylvania
x 5

Re: Publish-subscribe pattern

Post by skyforger »

Klaim wrote:It's because it's not called that way.
The Observer pattern ( http://en.wikipedia.org/wiki/Observer_pattern ) is not the Publish–subscribe pattern ( http://en.wikipedia.org/wiki/Publish%E2 ... be_pattern ).
If you want to make an apple pie from scratch, you must first create the universe. ~Carl Sagan
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Publish-subscribe pattern

Post by Klaim »

It's the same idea, one focused on observing one entity's event, the other on observing specific event types occurences.
Whatever way you got, there are also other concerns : does it have to be direct notifications? do they have to be buffered? thread-safe? etc.
User avatar
skyforger
Halfling
Posts: 49
Joined: Tue Apr 13, 2010 10:23 am
Location: Transylvania
x 5

Re: Publish-subscribe pattern

Post by skyforger »

Klaim wrote:It's the same idea, one focused on observing one entity's event, the other on observing specific event types occurences.
Mostly, you are correct. But the difference is: the publishers are unknown to the listeners, and the listeners only subscribe to a class of publishers, without knowledge of what they may actually be. They are more loosely coupled than the Observer pattern.
If you want to make an apple pie from scratch, you must first create the universe. ~Carl Sagan
User avatar
Klaim
Old One
Posts: 2565
Joined: Sun Sep 11, 2005 1:04 am
Location: Paris, France
x 56

Re: Publish-subscribe pattern

Post by Klaim »

Yes, and there are tradeoff to take account about because none of those solutions is perfect.