Although the Observer Pattern is a very powerful design, I'm not convinced it is appropriate in this situation. If you only have 2 Observers (both paddles), and they will ALWAYS be Observing the Subject, and there will not be any new Observers added or removed, then implementing the pattern might be adding too much complexity.
But, to create a simple Subject/Observer relationship, here's an idea:
For re-usability, create an Observable class:
class Observable { private: ObserverList list; // Some STL container public: void AddObserver(Observer o){ list.add(o); } // Observer will also be manually created void RemoveObserver(Observer o){ list.remove(o); } void RemoveAllObservers(){ list.clear(); } // This function is the key to the observer pattern; you can either use the //"Push" form by sending to the Observers a piece of data, or use the "Pull" //form by only sending yourself (the Observable), and letting your Observers //"pull" the information they need from you. These "pull" functions are simply //getter methods provided by the class that is inheriting this class. void NotifyObservers(Anything* anyData){ for_each(Observer o in list){ o.update(this, anyData); } // Push void NotifyObservers(){ for_each(Observer o in list){ o.update(this); } // Pull};
Now, you create an Observer class:
class Observer { public: virtual void update(Observable o, Anything* someData) = 0; virtual void update(Observable o) = 0;};
Now, your EventHandler would inherit from Observable, and your Paddle would inherit from Observer. Then, you would have to define the update methods in your Paddle class. One way you could do this would be to do something like this:
class EventHandler : public Observable { private: bool upPressed; bool downPressed; bool wPressed; bool sPressed; public: bool isUpPressed(){ return upPressed; } bool ... // Whenever a key is pressed, you want to update your bool values, and then notify all your Observers that a change has been made void handleEvents(){ //all your stuff here this->NotifyObservers(); // Pull method};
Now in your Paddle class
class Paddle : public Object, public Observer { public: void update(Observable o){ // You're going to have to do some type-checking here, but the idea is if(o.isUpPressed()) this->move(0, -1); } };
REMEMBER, you must add the paddles as Observers to your Observable, or else nothing will happen. So, in main, after you've created your Observable and Observers, just do this:
int main() {...EventHandler eh;Paddle players;Paddle cpus;eh.AddObserver(players);eh.AddObserver(cpus);...}
Now whenever EventHandler changes (a key is pressed), the paddles will be notified and they can re-act accordingly (however you define their update() functions).
If you wanted to use the "Push" method, you could send a Vector2(x,y) as the Anything* someData after calculating the sum of the forces, but then this information would be given to ALL observers, not just the player's paddle, so it would move the CPU paddle in the same direction, so you wouldn't want to do this. Or, just have 1 Observer, the player's paddle.
Now you have 2 re-usable classes, Observable and Observer, that you can use for all sorts of things. Re-usability! [smile]
Hope this gives you some more ideas. Now that I've spent all the time writing this up, I've convinced myself that this would be an interesting and fun thing for you to implement! This achieves your re-usability, and you're no longer sending a message from the EventHandler to the Paddle telling it to move; instead this allows the Paddle to handle its own movement. Now, you can add collision detection in the Paddle's move() function, without having to modify your EventHandler. Have fun!
[Edited by - Shakedown on March 5, 2008 1:04:02 PM]