GeeCON Prague 2014*
The moose likes OO, Patterns, UML and Refactoring and the fly likes different approaches to MVC Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "different approaches to MVC" Watch "different approaches to MVC" New topic
Author

different approaches to MVC

Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

<HYPERLINK INTENTIONALLY DELETED>

I thought that the View should collect some data and send it to the Model.
Is my interpretation correct? :



I think that my interpreation has less dependencies. View and Model know nothing about each other.

Scenario:
- user input some data. This data is collected by View.
- View passes that data to the Controller.
- Controller gives that data to the Model
- Model performs some calculation and send the result to the controller
- Controller sends the result to the View.

So in my interpretation - controller notify the View about updating.

Let's discuss. What do you think.

SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
- user input some data. This data is collected by View.


Yes, this is how is works.

- View passes that data to the Controller.


In addition to passing the data, there is also some "action" or "behavior" which is invoked within the Controller. A user clicks on a Submit button, for example.

- Controller gives that data to the Model


Yes, this is how it works. Controller may create Transfer Objects with the data or reformat the data depending upon how it is received.
In addition to passing the data, there is also some "action" or "behavior" which is invoked within the Model application.

- Model performs some calculation and send the result to the controller

Yes, this is how it works. Keep in mind that the Model application shouldn't be invoking methods on the Controller. So, there is no need for a Controller reference in your Model.

- Controller sends the result to the View.

Yes, this is how it works.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

James Clarks wrote:

- View passes that data to the Controller.


In addition to passing the data, there is also some "action" or "behavior" which is invoked within the Controller. A user clicks on a Submit button, for example.

OK, let's say that a user clicks a Submit button. Can I invoke that action after clicking (in MVC concept)? :



James Clarks wrote:
- Model performs some calculation and send the result to the controller

Yes, this is how it works. Keep in mind that the Model application shouldn't be invoking methods on the Controller. So, there is no need for a Controller reference in your Model.

OK, so keeping the reference to the View (instead of Controller) in the Model is a good option? After performing some calculations, model will notify the View to update.

Am I right?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
OK, let's say that a user clicks a Submit button. Can I invoke that action after clicking (in MVC concept)? :


First, the Model-View-Controller design pattern is a "pattern". A "pattern" does not concern itself with specific implementation details such as "a user clicks a Submit button."

In regards to our theoretical, simplified implementation, if a user invokes the Controller action by "clicking on a button", there is no way for a user to invoke the action after he already has, except by "clicking on the button" again if the UI is designed this way.

OK, so keeping the reference to the View (instead of Controller) in the Model is a good option? After performing some calculations, model will notify the View to update.


No, the Model application should not have ANY dependencies on the Controller or the View. Behavior in the Model is invoked by a Controller and responds to the invocation be sending data back or some kind of response code indicating that it has executed the behavior.

A Controller has a reference to something in the Model. A Controller has a reference to something in the View.

The Model application does not have any reference to a Controller or a View.

All of the intelligence involving View and Model interactions is coded in a Controller. Controller has intimate knowledge of View and how to invoke operations on Model.

The ultimate goal of MVC pattern is the ability to easily change or add new Views to the Model, WITHOUT having to make any changes to business logic in the Model. NO CHANGES IN MODEL. NOTHING...
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks for reply.
Please, consider this improved concept.



Scenario:
- user fills the form up and clicks the button,
- void buttonClicked(...){ //this method is from the view
//create data object from the form
controller.compute(dataObject);
}
- controller request the model to compute and get computed data:
computedData = model.getComputedData(dataObject); //this method is from the controller
- controller notify the view:
view.update(computedData);

Is it good approach? And is that in term with MVC?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
How about?

Controller:

public ActionForward executeAction(Action act, Object data);


Model:

public final Object compute(Object obj);


I suggest that you learn the Struts Framework. It is a very good example of a Controller and a View and will help you build a MVC-based application.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks. Can I use enum as Action?
So my approach is totally bad?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
Can I use enum as Action?


You could certainly try. If it doesn't work out then try something else.

In regards to your approach, if you can execute business logic (Model) from a command-line, i.e. standalone application, and if you can execute the same business logic from a HTML web page, then you have implemented MVC properly.

Repeated for emphasis...

The ultimate goal of MVC pattern is the ability to easily change or add new Views to the Model, WITHOUT having to make any changes to business logic in the Model. NO CHANGES IN MODEL. NOTHING...
Jelle Klap
Bartender

Joined: Mar 10, 2008
Posts: 1763
    
    7

James Clarks wrote:
OK, let's say that a user clicks a Submit button. Can I invoke that action after clicking (in MVC concept)? :


The Model application does not have any reference to a Controller or a View.



That needn't always be true. For instance, for a rich client application implemented using Swing or some such framework, it might make sense to use the Observer/Observable paradigm. Using the default implementation in java.util you could have the model class(es) (extend Observable), have the view and controller classes implement the Observer interface and have both views and controllers register themselves to the model. That way that model does have a reference to the controller, except that it doesn't know it's a controller, but rather just some registered observer wanting to be notified of state changes.

Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

Thanks for help! I am slowly entering into the world of OO
 
GeeCON Prague 2014
 
subject: different approaches to MVC