File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC and placement of timer-driven code Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC and placement of timer-driven code" Watch "MVC and placement of timer-driven code" New topic
Author

MVC and placement of timer-driven code

Kevin McMahon
Greenhorn

Joined: Nov 03, 2008
Posts: 19
Hi all.

I'm developing an app which has a list of steps. The steps execute sequentially.
Some steps have a value which represents a length of time (countdown timer) which should be waited after the main work of the step has completed, and before the next step is run.

There should be notification from the user to acknowledge the end of this countdown - like an alarm with an off button, I guess.

My question is - should the timer/countdown code be placed in the view of an MVC architecture? For example, if I'm using Swing for my view, I could use Swing's Timer to coundown, then sound and alarm and/or present a button to the user to click to move on to the next step.
However, if this is using the web, I'm limited with options to display the countdown (JavaScript in the browser, perhaps?)

Would this be the correct way to model this? Or should the timer be in the model somewhere, with some sort of event to trigger changes in the view layer? Having the timer in the view seems to make more sense to me?

Anyone offer any opinions on this?

Cheers,

Neilos
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
The steps execute sequentially.


This is a business requirement. The timer that manages this should be part of the business object model (Model).


There should be notification from the user to acknowledge the end of this countdown - like an alarm with an off button, I guess.


If this countdown is related to the timer managing the "steps", then it should still be part of the business object model (Model).

It is unclear whether the human user is controlling the execution of the steps and using "this countdown" as a measure, or, if the execution of the steps is independent of the user.

If you code the timer function in the View, you create a severe dependency upon the View technology and increase the level of coupling between the business object model (Model) and the View. This is not what the Model-View-Controller design pattern is about.

Ideally, the timer should be in the business application (Model) and send information to presentation application (View).


Kevin McMahon
Greenhorn

Joined: Nov 03, 2008
Posts: 19
Yes, I agree that initially the model seems like the correct place, but then how to you get the fact that the timer has finished to feed back to the view without coupling it?

To clarify, steps must run, but before they finish, the user is prompted to continue.
So, for example, we have 3 steps. Step 2 has a 30 second timer. Steps 1 and 3 have no timer.
A quick example process would be:

Step 1 - displays itself, runs, provides "next" button to the user.
User clicks button.
Step 2 displays iself and runs. 30 seconds later, an alarm/notification/something is given (presume a sound for now)
A "next" button is again displayed. The user clicks the button to silence the alarm and start the next step.
Step 3 displays, runs, provides a "next" (or "finish") button to the user.
User clicks button, app exits.

If the timer is in the model, and you are using a web client, how (apart from using something like reverse AJAX) would that work??? When the timer hits the time, I can't see how to notify the view that this is the case without coupling the layers tightly.

If the timer is in the view, at least when it reaches 0, the "next" button can be displayed without calling the model, and the event of clicking this can be handled in the usual way (passed to controller which calls next step from the model etc..)

Does that make sense?
Jimmy Clark
Ranch Hand

Joined: Apr 16, 2008
Posts: 2187
A lot depends upon the View technology. Some things are not appropriate for HTML-based web browser.

The decision to implement the Model-View-Controller design should be based upon technical and non-technical requirements,
including presentation and business requirements.

Basic HTML controls are for sending data to web server and receiving data from web server. If you are attempting to efficienlty monitor a
server-side process from a HTML client, you will need something more that basic HTML controls. One option is simply to let the web page
"hang" until the server completes the step. When the step completes, it will send data back to client. Users can then click next button and wait for server response. If having the page "hang" is not sufficient, you would need something more than basic HTML controls.

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: MVC and placement of timer-driven code