4

I'm still trying to understand what is the correct way of implementing MVC. This example @oracle says that view has access to the controller. And another tutorial @leepoint is indicating that the view has access to model. Are these different variations of the the MVC? In my case I was following the tutorial at Oracle site with some modifications(I have added a function in AbstractController getModelProperty, which will allow me to retrieve the value of the fields of the current registered models, but it also could me sense to pass the model as parameter(like indicated at leepoint tutorial) to simplify and probably optimise the data access for the view.

Thanks in advance.

1

5 Answers 5

2

Views are bound to models. Since views render models, they have to have intimate knowledge of the model, there's simply no way around it. Some views are generic, and these have "generic" models. Here, you may try and conform your actual model to the generic one so that the "generic" view can use your data. But even with these generic models, the views are still tightly bound to them.

Models manage data, the state. While a view has intimate knowledge of the model, the model is view agnostic. It simply doesn't care. This way you can have several views for the same model.

However, a model must inform others of changes to the model. Typically in java you use PropertyChangeListener's. This mechanism lets the model just shout out changes wholesale, and anyone interested can listen for these changes and act on them, for example your view.

A simple example is that you game object can take damage from a bullet, and it's reduced to below 50% health. The view can see that health has been reduced and change the image of the model (say adding smoke, or whatever).

The Controller typically is bound tightly to the view and the model. It knows the capabilities of the view (like it's size, and other areas of interest), and it knows how to change the model. Such as when mouse is clicked, the controller converts the mouse point in to a coordinate relative to the view, and from that determines what object was clicked. Once it determines the object that was clicked, it can set the model for the object to, say, "selected".

The model then broadcasts out that it's "selected" property has changed. The view sees this, finds the bounding rect for the model that changed, and invalidates that rectangle on its display.

Finally, Java comes around and tells the view "Hey, Rect 10,10,100,100 needs to be painted". And the view find the models in that rect, paints the new view of the object with a "selected" border, or whatever.

Thats how the whole cycle works.

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

1 Comment

Very nice and simple explanation. From the previous answers and comments I understood that both of the examples are correct, but the best way of implementing the MVC depends a lot on situation, there is no generic solution. I mean the solution is "generic" but can vary little bit depending on the problem.
0

It's both. MVC has a triangular relationship with each other, with the Controller on the top. The newer way to go is using MVP, where the Presenter sits between the model and the view.

It's better if you can keep as much of the model knowledge out of the view and only feed it information specific to it's viewing task. It does make your life easier in the long run.

1 Comment

I'm making the game and after user successfully authenticates, I'm loading map from file and setting it to model, but on game cycles I will need to access it.. That's why I'm indecisive if I should pass the model to the view as parameter..
0

... says that view has access to the controller.

yes, the view holds a reference to the controller for user gestures. (in a gui, the view and controller sometimes end up lumped together).

... that the view has access to model.

yes, the view usually holds a reference to the model(s) (it might have more than one).

... Are these different variations of the the MVC?

there are a bunch of variations.

... but it also could me sense to pass the model as parameter(like indicated at leepoint tutorial) ...

usually the model has views that are observers and the view updates itself when it receives an update message as opposed to being called directly by the controller. the former decouples the view more from the controller.

3 Comments

I agree with you that there can be several Models and that's why it can be not very wise of passing model to the view as parameter.. But what if I need accessing constantly the object of one of the models?
Since the view can talk to the controller it can request the information it needs at the time and the controller can fetch it from the model.
the view should only need to access the model when it receives an update message so it can repaint itself
0

The Swing libraries are a very good implementation of the MVC pattern. Just study the API for a bit and it will all fall into place for you.

Comments

0

The Wikipedia Article on MVC nails the relationships pretty well: View has the Model, Controller has both the View and the Model, Model knows nothing about both View and Controller.

In some cases it may be advantageous to simplify the pattern by combining View and Controller.

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.