1

I'm starting to learn to program and I created a simple BlackJack console game. Now I'm trying to create a GUI using swing/JFrame and decided to try and learn basics of MVC at the same time. I came across this java / gwt UI coding - clean code about MVC

My questions is how exactly should the View be notified of a change to a players hand after they hit "hit."

If user wants to hit, hitBtn(in VIEW) -> actionListener()(in CONTROLLER) -> hit()(in MODEL)

 - hit() would than modify the nessessary model classes 
   (i.e. DealtCards, Deck, Cards objects)

How should the view know the Model has changed and more basically how should the View display the data in the model classes, for example the players hand?. Using getter methods in the model classes or a pass through from Model to Controller to View? (I'm under the impression the View shouldn't have any reference to the Model)

Thanks!

2 Answers 2

1

For notifying the view about the model changes:
1. PropertyChangeListener
2. The controller can act as a bridge between the view and the model propagating the changes.
I don't think it is always unacceptable to not refer from the view to the model. If you have a clear interface for the model types and the actual implementation is hidden, the view may refer to it.
Also take a look at data binding, which is SWT though, but the methodology is implementation independent.

Sign up to request clarification or add additional context in comments.

Comments

0

There's no problem in the view having references to controller, nor controller holding references to view. It has to be that way for the thing to work. The important thing is how you pass the references to each one for them to be loosely coupled. I usually use references to interfaces and constructor or setter injection to perform the binding. More about Inversion of Control in this link:
http://martinfowler.com/articles/injection.html

Basic idea for MVC is:

  • Code interfaces for model, view and controller.
  • Code implementations for model, view and controller.
  • Decouple them as much as possible.

A tip about the controller: I usually put the ActionListeners and other specific GUI classes in the view implementation, and from there I dispatch my own events to the controller. This way, if I want to port the application to other platform which has a different GUI, I can reuse the view interface, the controller interface and probably the controller implementation. The only ones that are not often reusable are the view implementation and the model implementation.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.