Sign in to follow this  
Tibre

Message Passing, Delegation, Inheritance

Recommended Posts

Hi. I'm working on a system right now. My idea is that every object in the system responds to messages. These messages are handled through a ProcessMessage() function defined in a Messager interface that every object implements. I'm having some troubles with this though. Specifically... An object in the game currently may implement other interfaces based upon its capabilities. For instance, a visible and movable object may implement the Drawable interface. This interface handles movement and rendering functions, and provides member variables appropriate for that, and default methods to handle those messages. That same object may also implement an ai interface. This interface handles update and other messages relevant to it performing its necessary ai routines, and has default member variables and member functions appropriate for that. Unfortunately, of course, this object won't have one ProcessMessage() function that handles both sets of messages, but instead will have 2 separate ProcessMessage() functions. I considered having the object have its own ProcessMessage() function as well, and delegate the message onto the appropriate interface, but that seemed to just be masking the real issue. Not to mention it felt a little like I was smashing a square peg into a round hole. I did some looking around and read about Delegation. With this, it seems that instead of inheriting from multiple classes, I would instead have a list of delegates (pointers to other classes) that I would send appropriate messages to. If the object doesn't have an appropriate delegate, then it can't handle the message. If I want to change the way the object handles messages, I just swap out the delegate (and transfer over pertinent data). I can also dynamically add or remove the ability to handle classes of messages. This seemed to be much more elegant. However, I haven't really heard too much about this. So I'm curious if there is something very simple that I am just overlooking. I would appreciate any feedback on this. Thank you.

Share this post


Link to post
Share on other sites
The Delegate pattern is actually quite common in .NET, where it's used all the time as an event-handling mechanism. It's basically a list of function objects, and each time an event is generated you search the list for the appropriate function object and call the event handler. A simplistic C++ implementation can be found here, assuming you use C++ on a native platform.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this