Question

I'm writing a small card game and I'm stuck. Just some basic: 1) view observes the model 2) controller decides when to show output window and when to show input window 3) model does all math

Well, it looks like my controller has to be an observer of model, which is bad and, as far as I know, also wrong.

My situation: there comes a moment when user has to make a choice and confirm it via command line.

char View::getPlayersChoice
{
while ( cin>>choice )
{
   if ( choice == 'y' || choice == 'n' )
     controller.getPlayersChoice( choice )
   else
     cout<<"Invalid input. Please try again."
}
}

So somehow my controller has to know when the moment has come and call the View's function getPlayersChoice. And the only way I see it: 1) model needs some user input 2) model tells the controller: "Hey, give me input!" 3) controller says "Ok" and calls view.getPlayersChoice 4) controller gives input to model

But again, model should know nothing about the controller and the view. So, how should I solve this problem? Thanks for help

P.S. There is an idea I have: maybe I should create something like events, and pass it as argument, like observer.update( eventThatHappened ). View gets the event, calls the appropriate method, but it's the controller's job to decide when user should enter info and when it view should show something.

No correct solution

OTHER TIPS

The worklow that can be used when implementing strict MVC or MVC-live architecural pattern is:

  • The view queries the controller
  • The controller asks model to do some processing
  • The controller notifies the view

As the view knows the controller but the opposite is not true, I use to implement the subject/observer pattern to communicate from the controller to the view.

Thus you have:

class View 
{
  void doSomething()
  {
    controller.requestDoSomething();
  }

  void onProcessingSuccessEvent()
  {
    // do stuff (for example read model)
  }
};

class Controller
{
  void requestDoSomething()
  {
    model.process();
    ...
    notify(ProcessingSuccessEvent);
  }
}

I hope this can help you with your issue.

Best Regards,

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top