Proper MVC implementation in Java

I'm still trying to figure out what is the correct way to implement MVC. This @oracle example says the view has access to the controller. And another @leepoint tutorial points out that the view has access to the model. Are these different variations of MVC? In my case, I followed the training course on the Oracle website with some changes (I added a function to AbstractController getModelProperty, which will allow me to get the value of the fields of the currently registered models, but I could also pass the model as a parameter (as indicated in the leepoint tutorial) to simplify and probably optimizing data access for presentation.

Thanks in advance.

+4
source share
5 answers

Views are tied to models. Since representations visualize models, they must have intimate knowledge of the model, there simply is no way. Some views are common, and they have โ€œcommonโ€ models. Here you can try to match your actual model with a common one, so that the "general" view can use your data. But even with these generalized models, views are still closely related to them.

Data management models, state. Although the view has a deep knowledge of the model, the model is agnostic. It is just the same. Thus, you can have several views for the same model.

However, the model must inform others of changes to the model. Usually in java you use PropertyChangeListener. This mechanism allows the model to simply shout about wholesale changes, and anyone can listen to these changes and act on them, for example, your opinion.

A simple example is that a game object can take damage from a bullet, and it has decreased to below 50% health. The view can see that the health has been reduced and the image of the model has changed (for example, adding smoke or something else).

The controller is usually closely associated with the view and model. He knows the possibilities of representation (for example, size and other areas of interest), and he knows how to change the model. For example, when you click the mouse, the controller converts the mouse point into a coordinate relative to the view, and it determines which object was clicked. When it determines the object that was clicked, it can set the model for the object, for example, "selected."

The model then reports that the "selected" property has been changed. The view sees this, finds the bounding box for the model that has changed, and invalidates that box on its display.

Finally, Java comes and says, "Hey, Rect 10,10,100,100 need to be drawn." And the look finds the models in this rectangle, draws a new view of the object with the โ€œselectedโ€ border, or something else.

This is how the whole cycle works.

+2
source

It is both. MVC has a triangular relationship with each other, and the controller is on top. A newer way to use it is to use MVP, where Presenter is between the model and the view.

It is better if you can save so much knowledge about the model from the view and only submit this information specific to it to view the task. It makes your life easier in the long run.

0
source

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

yes, the view contains a link to the controller for user gestures. (in gui, view and controller sometimes end together).

... that the view has access to the model.

yes, a view usually contains a reference to a model (it can have more than one).

... are these different variations of MVC?

there are tons of options.

... but also I could pass the model as a parameter (as indicated in the leepoint tutorial) ...

typically a model has views that are observers and update views when they receive an update message, rather than being called directly by the controller. the first separates the view from the controller anymore.

0
source

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

0
source

The Wikipedia article on MVC connects relationships well: View has a model, the controller has both a view and a model, the model knows nothing about the view and the controller.

In some cases, it may be beneficial to simplify the template by combining View and Controller.

0
source

Source: https://habr.com/ru/post/1341244/


All Articles