Search code examples
design-patternsnetwork-programmingobserver-pattern

Using Observer pattern over a network for board game


I am looking to make a networked board game based on Risk in C++. My idea was to have a central server which hosts a game lobby where users can connect and make/join games. The Observer pattern seems attractive in this case, since I could host all the game model/logic on the server, and the clients would just be observers to this and display the current game state using a view.

My first question: Is this approach possible? Most of what I've heard/thought is that the clients have their own game models. However I'm thinking for a game that's not computationally intensive, a single model hosted by a server would have advantages (no out of sync issues, prevents cheating, etc.).

My second question: How would I go about implementing the Observer pattern over a network? Since I can't make a direct method call over the network, I would need some kind of easy way to simulate this using data. Would there be more advantages using a "pull" (observer requests updates to game data) or "push" (server pushes out new updated data to all clients) approach?


Solution

  • From your problem statement, it seem that you need to implement Distributed Observer Pattern or Distributed Publish / Subscribe. PubSub is a messaging paradigm and can be easily implemented with a MOM (Message Oriented Middleware) - ( see RabbitMQ, ActiveMQ, OpenMQ) where the MOM does the heavy lifting.

    XMPP can also serve your purpose right(see XEP-0060). All you need is a Jabber server and a C++ XMPP library (gloox is a good one and supports XEP-0600).

    You might also be interested in pubsubhubbub.