Question

I have been following the modified Model View Controller example presented in this article by Oracle. Java SE Application Design with MVC

In this example the DefaultController class declares property name strings like this:

public static final String ELEMENT_TEXT_PROPERTY = "Text";

These strings are used in multiple places.

In the controller:

AbstractController uses reflection to search its models for a method named set+propertyName.

In the Model:

The model has setter methods which call this method:

firePropertyChange(Controller.NAME_PROPERTY, oldName, name);  

In the View:

In the modelPropertyChange method,

if(evt.getPropertyName().equals(Controller.NAME_PROPERTY)

The part that concerns me is that the model is referencing the controller's static fields. I am still trying to master MVC and I am unsure if this architecture achieves the desired decoupling that MVC is used for. Does it matter that the all three classes reference the controller's static fields?

EDIT: Is the architecture described in this tutorial, as coded, an invalid representation of MVC? If so, can it be altered so that the model / (model layer) is not dependent on the static property names defined in the DefaultController class?

I realize this article is "just a tutorial", but if the code presented in it does not reflect the claimed decoupling, I think that the community should stop referencing it as an example of MVC.

Was it helpful?

Solution

I am not Java (or even desktop application) developer, so take this all with grain of salt.

First of all, static fields in any code represent a global state, which is completely against any practices in object oriented code.

As for your worries, that the code in tutorial violates SoC, you are right. But then again , it is intended as a basic tutorial, and not something too advanced. The goal is to show the parts of MVC triad and give at least surface level comprehension of how they interact and what are the main responsibilities. It is not meant to be copy-pasted in production code.

Once clear sign for it would be, that there exists a "model object" in this code. In a real world situation model would be a layer.

If you want to gain more extensive understanding of MVC, i can recommend three reading materials:

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