Original Post
Hi,
I've been wondering about this for around a week now, and I'm still not quite sure how to solve it. I come up with different ideas every once in a while, but none of them seem to be "The Best" way of doing it, and some have downright ugly disadvantages.
So here's my question. I've got an Event Management System that allows EventListeners to register with the EventManager, and that allows any class in the system to dispatch an Event. These events can be of an arbitrary number of different types, including but not limited to Movement Events (by both the Player and AI), State Change Events, Game Exit Events and Interaction Events. All of these events need their own specific body, for instance, a Movement Event might need a source (The object that does the moving) and a direction vector. A State Change Event needs to know about the next state. Game Exit Event doesn't need any information, and an Interaction event needs a source and a destination object.
This diversity forms a problem for me, as the first structure I thought of was having an abstract class "Event" and an abstract class "EventListener". EventListener has a method called "Notify" and "handleEvents", and any class that needs to be an event listener can simply inherit those methods. Any Event will just be a subclass of the "Event" class. Having simply one notify method would mean that Casting has to be used, as far as I can see, and I want to avoid that.
A solution to casting I thought of is to have many different "Notify" methods, one for each event. Each of these would have an implementation in the EventListener interface (though the implementation would be to just do nothing with the event), and inheriting classes would simply override the one they're interested in. This would leave the interface rather bloated, with a lot of code, and not a lot of flexibility.
Other options I came across were void pointers, which again imply casting; Further abstracting the Event objects, making a more defined structure and being able to split the events at a higher level, but this would make notification more complicated due to code duplication.
I'd love to hear how you guys approach this problem, as I'm running out of ideas. I'm not looking for a "Quick Hack" as you might've already figured out, as I want the fundamental design to be solid and high quality for this project.
I've been wondering about this for around a week now, and I'm still not quite sure how to solve it. I come up with different ideas every once in a while, but none of them seem to be "The Best" way of doing it, and some have downright ugly disadvantages.
So here's my question. I've got an Event Management System that allows EventListeners to register with the EventManager, and that allows any class in the system to dispatch an Event. These events can be of an arbitrary number of different types, including but not limited to Movement Events (by both the Player and AI), State Change Events, Game Exit Events and Interaction Events. All of these events need their own specific body, for instance, a Movement Event might need a source (The object that does the moving) and a direction vector. A State Change Event needs to know about the next state. Game Exit Event doesn't need any information, and an Interaction event needs a source and a destination object.
This diversity forms a problem for me, as the first structure I thought of was having an abstract class "Event" and an abstract class "EventListener". EventListener has a method called "Notify" and "handleEvents", and any class that needs to be an event listener can simply inherit those methods. Any Event will just be a subclass of the "Event" class. Having simply one notify method would mean that Casting has to be used, as far as I can see, and I want to avoid that.
A solution to casting I thought of is to have many different "Notify" methods, one for each event. Each of these would have an implementation in the EventListener interface (though the implementation would be to just do nothing with the event), and inheriting classes would simply override the one they're interested in. This would leave the interface rather bloated, with a lot of code, and not a lot of flexibility.
Other options I came across were void pointers, which again imply casting; Further abstracting the Event objects, making a more defined structure and being able to split the events at a higher level, but this would make notification more complicated due to code duplication.
I'd love to hear how you guys approach this problem, as I'm running out of ideas. I'm not looking for a "Quick Hack" as you might've already figured out, as I want the fundamental design to be solid and high quality for this project.
