0

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.

Justin Wiseman
  • 780
  • 1
  • 8
  • 27

1 Answers1

1

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:

tereško
  • 58,060
  • 25
  • 98
  • 150
  • Thanks for the reply. I am not familiar with the term "layer" as you used it. What is that? In java, everything is an Object. I am not sure why there being a model object in the example would be incorrect. – Justin Wiseman Aug 01 '12 at 01:04
  • [This definition](http://en.wikipedia.org/wiki/Layer_(object-oriented_design)) seems to fit well to describe the how I use it. I meant, that there should not be a class or object containing word "model". The model layer is a conceptual boundary, which contains different kinds of classes, each with specific responsibilities. And all these classes, when combined, describe application's domain business logic. – tereško Aug 01 '12 at 02:04
  • Since the controller in this example contains a reference to the model, could the model's dependency on the controller's static fields be removed by moving those fields to the model? One thing I tried was creating an enum in the model that contained the model's field names. This enum was accessible by the controller and in turn passed from the controller to the view(s). By doing this, I think that the model can be ignorant of any controllers or views. Do you agree? – Justin Wiseman Aug 02 '12 at 14:19
  • Controller has a reference to "model" for one specific reason: to change the state of it. When you write a controller, you tailor if for the model layer. Model layer structure should not contain any public valuables (especially not made just to accommodate controller). Communication with model layer should happen through methods, not by breaking the encapsulation. – tereško Aug 02 '12 at 14:54