this is kind of a follow up question to this http://stackoverflow.com/questions/23743285/model-view-controller-and-callbacks, but i wanted to post it seperatly, cause its kind of a different topic.This is kind of a follow up question to this http://stackoverflow.com/questions/23743285/model-view-controller-and-callbacks, but I wanted to post it separately, because its kind of a different topic.
I'm working on a multiplayer cardgame for the androidAndroid platform. I split the project into MVC which fits the needs pretty good, but imI'm currently stuck cause i cantbecause I can't figure out a good way to communicate between the different parts.
I have everything setup and working with the contoller beeingcontroller being a big state machine, which is called over and over from the gameloop, and calls getter methods from the guiGUI and the android/network part to get the input. The input itself in the guiGUI and network is set by inputlisteners that set a local variable which iI read in the getter method.
So far so good, this is working. But my problem is, the controller has to check every input seperatlyseparately,so if iI want to add aan input iI have to check in which states its valid and call the getter method from all these states. This is not good, and lets the code look pretty ugly, makes additions uncomfortable and adds redudanceredundance.
SOSo what iI've got from the question iI mentioned above is, that some kind of command or event pattern will fit my needs. What iI want to do is, to create a shared and threadsafe queue in the controller and instead of calling all these getter methods, iI just check the queue for new input and proceed it. On the other side, the guiGUI and network dontdon't have all these getters, but instead create an event or command and send it to the controller through, for example, observer/obsererableobservable.
Now my problem: I cantcan't figure out a way, for these commands/events to fit a common Interfaceinterface (which the queue can store) and still transport different kind of data (button clicks, cards that are played, the player id the command comes from, synchronization data etc.).
If iI design the communication as command pattern, iI have to stick all the information thatsthat is needed to execute the command into it when its created, thatsthat's impossible causebecause the guiGUI or network has no knowledge of all the things the controller needs to execute stuff that needs to be done when for example a card is played.
I thought about getting this sutffstuff into the command when executing it. But over all the different commands iI have, iI would need all the information the controller has, and thus give the command a reference to the controller which would make everthingeverything in it public, which is real bad design iI guess.
So, iI could try some kind of event pattern. I have to transport data in the event. So, like the command, iI would have an interface, which all events have in common, and can be stored in the shared queue. I could create a big enum with all the different events that a are possible, save one of these enums in the acctualactual event, and build a big switchcauseswitch case for the events, to proceed different stuff for different events. the
The problem here: iI have different data for all the events. But iI need a common interface, to store the events in a queue. How do iI get the specific data, if iI can only access the event through the interface ?
Even if that wouldntwouldn't be a problem, imI'm creating another big switch case, which looks ugly, and when i want to add a new event, iI have to create the event itself, the case, the enum, and the method thatsthat's called with the data.
I could ofcourseof course check the event with the enum and cast it to its typtype, so iI can call eventtypeevent type specific methods that give me the data iI need, but that looks like bad design too.
I'm thankful for any input on this.
Thanks in advance.