Original Post
I would like to use a component-based entity system for my next project. Let's say I have a "Ship" entity with the following components: - RenderComponent - FrameComponent (size and position) - CollisionComponent - InputComponent etc. I thought of using the Observer design pattern for communication between the components of the game's entities. Each component would be an observer and a subject at the same time; components would notify all other components of the entity (and possibly components of other entities as well, like a health bar in the HUD for instance). The FrameComponent would use the InputComponent's notifications to update the ship's position. The CollisionComponent would then use the position update to check for collisions against enemy bullets. The RenderComponent would also need the position update notification to draw a sprite at the correct location on the screen, etc. You get the point. Now, there's something that has always bugged me with the Observer pattern, that is, when a single observer wants to be notified of updates by multiple subjects (RenderComponent needs to be notified when the position of the ship changes [FrameComponent] and when the ship explodes after a collision [CollisionComponent]). Most implementations of this pattern simply pass a pointer to the subject to the observers' notify() method : The "problem", if it is one, it what happens at the other end, in an observer's notify() method. Of course, I could use a giant switch case using something like a ComponentType (or something like a dynamic_cast<> in C++) to determine what type of subject just sent a notification. That solution doesn't seem very flexible nor elegant to me. What would be a better solution? Am I even using the right approach to the problem? Thank you for your suggestions and ideas.
foreach(observer in observers)
observer->notify(this);