Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / October 2006

Tip: Looking for answers? Try searching our database.

MVC Pattern or antipattern?

Thread view: 
Daniel Pitts - 23 Oct 2006 23:38 GMT
I have a domain model which is a simulation.  I want to display the
state of the simulation at any given time, basically animated.

Since the Swing threading model requires all GUI work to be done on the
EventDispatcher thread, and all other long-running work to be done on
some other thread, I have a delima.

It might take some time to render a single "frame" (snapshot of the
domain model), and its desirable that rending doesn't interfere too
much with the simulation (not a strict requirement). Similarly,
updating the domain could take some time, and is not in any way atomic.
I'm trying to decide one the best approach to this problem...

I can think of three approaches:
A: Atomize the simulation thread. Make the simulation "pause" when the
painting starts.
B: Inline the simulation thread. Make the "updateSimulation" occure in
the event dispatcher thread.
C: Create a seperate immutable model that is a snapshot of the
simulation at reasonable time boundries, and pass this to the rendering
code through the event dispatchers.

I think solution C is the best solution, but I think there would be a
lot of overhead, as every simulation "snapshot" might not be used, and
isn't necessarily cheap to create.  I'm sure I could work around this,
but if there is a better approach, I'd like to know about it before I
continue.

Question part 2:
My model really would know how to represent itself in the view, so
might it be useful to have my business objects implement a paint()
method (or the equivelent).
Oliver Wong - 24 Oct 2006 14:54 GMT
>I have a domain model which is a simulation.  I want to display the
> state of the simulation at any given time, basically animated.
[quoted text clipped - 23 lines]
> but if there is a better approach, I'd like to know about it before I
> continue.

   How about C, but instead of the simulation creating a snapshot at
regular intervals, the View requests a snapshot, and the simulation will
respond to it at the next convenient point in time? I'm assuming the
simulation is running in a seperate thread from the whatever powers the
view. I'm also assuming that it's more important for the simulation to run
undisturb than it is to get nice smooth framerates for your animation.

   If this solution isn't acceptable, you should probably mention what kind
of time scales you're talking about. The solution is obviously different if
your simulation's time boundries occur once every 2 hours, and rendering
takes 3 minutes, versus your simulation's time boundries occur every 50
milliseconds, and rendering takes 1 second.

> Question part 2:
> My model really would know how to represent itself in the view, so
> might it be useful to have my business objects implement a paint()
> method (or the equivelent).

   Wouldn't this defeat the whole purpose of using MVC? The idea is to
isolate the view from the model so that you can very the view. If you know
you're  never going to vary the view, there isn't much to gain from using
MVC.

   - Oliver
Daniel Pitts - 24 Oct 2006 19:32 GMT
> >I have a domain model which is a simulation.  I want to display the
> > state of the simulation at any given time, basically animated.
[quoted text clipped - 48 lines]
>
>     - Oliver

Yeah, and now that you meantion it, the controller is pretty much the
same as the model.  I once read on WardsWiki that OOP and MVC are
opposing design methodologies.  There was a bit of debate about it, and
it occures to me that it is difficult (I won't say impossible) to
reconsile MVC with tight encapsulation.

As for the view requesting the snapshot, that seems like a greate idea.
The timescales for both the simulation and the view are in the
miliseconds (hopefully).  A nice animation is desirable, but having it
decoupled from the actual simulation is also a design goal.  The
original program, which I'm porting to Java, was written in Pascal and
has things like "if graphicsEnabled then drawRobot(x,y,foo,bar)"
directly in the business logic. Except, not all the variable/function
names or as readable.

So, the solution that I think makes sense now:

Main Thread: Simulation is running...
Event Thread: View is done painting and wants a new frame. Sets an
atomic flag "viewNeedsSnapshot" (or something like that)
Main Thread: Simulation finishes iteration, checks flag. Produces
snapshot if appropriate and sends event "updateView(snapshot)" (or
something like that)
Event Thread: updateView event recieved, sets the snapshot as the
to-be-rendered object, and calls repaint()
Everyone continues on as usually.

Thanks for the help.
- Daniel.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.