This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes OO, Patterns, UML and Refactoring and the fly likes MVC - How Did You Do It? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » OO, Patterns, UML and Refactoring
Bookmark "MVC - How Did You Do It?" Watch "MVC - How Did You Do It?" New topic
Author

MVC - How Did You Do It?

Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
There have been some interesting discussions of MVC around here. I looked for more stuff on Google and ran across a couple ideas. I wanted to open the discussion here again to see how people have really done this stuff.
Here are two ways to implement the interactions:
<br /> In passive model (above) the model has no code at all to accomodate being used in MVC. It just does it's business / domain stuff and the controller has to know when visual updates are in order.<br /> <br /> In the active model (above) the Model publishes the Update message to all interested listeners. This has the advantage of supporting multiple controllers and views (maybe grid, pie chart and line graph) at the same time and simplifies the controller in a generic way.<br /> Here's another scenario (below) that works well with active or passive, but I showed it for active:<br />
Some questions for y'all...
  • How many of you really make a separate controller class?
  • Do you have rules like, only swing code in view, no swing code in controller?
  • Do you make all model changes through controller to model, or connect view to model? What do your swing listeners do?
  • Swing implements MVC internally. Anybody make your domain model and the swing model the same object?


  • A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    I found this Thread useful in inderstanding MVC.
    MVC
    As well as some discussions in the Developer forum notably Eugene Kononov's.
    Having a separate controller class makes your application easier to maintain.
    As does having a separate model and view.
    I've come across examples where the the View is separate from the GUI.
    The View are the classes common to all GUIs like Textbox,Table,Tree. But you can allow the option to render them either as HTML,Swing,etc and these make up the GUI part.
    You could have a JavaScript controller, a Java controller .
    I'm used to the scenario where the Model publishes changed model events and the View subscribes to them.
    (active I think)
    Can you list the advantages of your valid MVC models ? For example where the View gets the data directly .Perhaps you'd write fewer classes as you don't expect any new extensions to the GUI.
    regards
    [ June 27, 2003: Message edited by: HS Thomas ]
    Raj Waters
    Ranch Hand

    Joined: Apr 21, 2002
    Posts: 37
    How many of you really make a separate controller class?
    It all depends on how vast your application is in terms of business cases. One controller per business case is a better bet.
    Do you have rules like, only swing code in view, no swing code in controller?
    Ideally the controller should be a pure delegator.
    Do you make all model changes through controller to model, or connect view to model? What do your swing listeners do?
    Yes for dynamic views, model can notify the listeners
    Swing implements MVC internally. Anybody make your domain model and the swing model the same object?
    Does it mean using Swing components directly?


    - Raj<br />[MCSE, SCJP, SCWCD]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    I'm smilin here. Lots of people would say MVC is well understood, done to death. But here we have a plethora of options in only a couple replies. I'd love to see code snippets from real projects that people are proud to show off. I'll follow that link to see what's there.
    Re the Swing issue: You can make custom models for Swing controls. Could you use that as your Model component? Or do you have to copy data back & forth between the Swing model and the Model model?
    I'll throw some more gasoline on the fire.
    Classical Smalltalk definitions of MVC sometimes show GUI widgets as controllers. A text field or slider where you enter a value is not (exclusively) a view. I used a Smalltalk where even the graphs could be input widgets - you could drag bars and points and pie slices around to change the numbers (Model) behind the graph. Should a Swing widget be a controller?
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    HS, I'm being a bad boy, only throwing questions out, not sure I have any answers. I have one of yours tho - I think the view needs to "get" the data from the model when it opens because the model would have no reason to publish all of its data even though nothing has changed. After that, the pub-sub path looks better because it can notify multiple views.
    I'm not a big fan of that publish-get pair as shown in the diagrams. I usually try to have the publisher push the data out. I guess I got burned by a pub-sub package that had synchronization problems, so sometimes when the subscriber went back to "get" the data, it was already replaced by something else.
    Frank Carver
    Sheriff

    Joined: Jan 07, 1999
    Posts: 6920
    I'm not a big fan of that publish-get pair as shown in the diagrams. I usually try to have the publisher push the data out.
    I have tried this in the past, but found that it implies that the model either has to send everything that has changed in case the view needs it, or has to somehow know about what each view wants (greatly limiting flexibility).
    Take an email client. There might be a model holding emails, an "inbox" view listing available messages in the inbox, and a "new mail" view showing a lit up icon if there are unread messages.
    I suggest it would be foolish to send the full details of every incoming email to the "new mail" view, just for it to discard them all. A simple "inbox status updated" event would do the job, and allow both views to ask the model for just the information they need.
    [/i]I guess I got burned by a pub-sub package that had synchronization problems, so sometimes when the subscriber went back to "get" the data, it was already replaced by something else.[/i]
    I guess its down to different experiences. I'd consider that scenario as an advantage of the pub/sub model. When traffic is busy, only lots of small data-less events need to be sent, and a view always gets the most current status whenever it asks. It's quite feasible for a view to wait until a snowstorm of events dies down, then fetch the current status just once.
    This has helped me build much more responsive and friendly applications.
    Can you explain what problems you had with this approach.


    Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Re pub-sub - Your point about unpredictable subscriber needs is well taken. I can go either way (sounds kinky :cool . Swing events send along the data, but it's very small.
    BTW: I enjoyed the MVC discussion in the link above. Seems to be following a pattern recommended by a Sun workshop. I drew it like this:

    The view and GUI are separated as someone suggested above, but not as well decoupled as suggested above. That is, the View knows it's dealing with Swing. Still, I like the division. It lets Swing-based classes focus on Swing, and the View focus on translating events to functions.
    Debra called the message from view to controller "User Gesture". I'd say Swing Event and User Gesture mean the same thing to me, and the message to the controller should be a more abstract description of what the user wants to do. Aside from that, it's fitting what we expect from MVC quite nicely.
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    I'm told MVC is better understood if it is split into the patterns that make it up and each pattern is learnt individually.

    The Views use the Composite Pattern to form a tree of Views.
    The relationship between Views and Models is the Observer pattern.
    The Controllers are Strategies of the views.

    The Document View is more popular apparently than MVC (read it on Wiki so it must be true , or rather the statement was unchallenged ). DocumentView does not have the Strategy pattern (Controller) as in the case above where the View "gets" the data from the model.

    regards
    [ June 27, 2003: Message edited by: HS Thomas ]
    Lasse Koskela
    author
    Sheriff

    Joined: Jan 23, 2002
    Posts: 11962
        
        5
    I wonder why the kind of graphs like Stan and HS drew aren't included in every pattern book explaining MVC... Good job.


    Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    I certainly appreciated Stan's. Worth going over a few times. Mine is a visual representation of Model View Controller as an Aggregate Design as described on Wiki. I certainly found THAT useful.
    It's a pity you can only post diagrams with code symbols and not real Model diagramming
    symbols .
    regards
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    Sequence diagrams are about my favorites. I made a little REXX tool to generate text diagrams so I can cut & paste them into any kind of doc with no graphics.
    The GoF pattern book has about the same description of MVC as built out of other patterns. We haven't even started to talk about the static relationship between these things, only the dynamic!
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    Sequence diagrams are about my favorites. I made a little REXX tool to generate text diagrams so I can cut & paste them into any kind of doc with no graphics.

    Would it work here? gets slightly annoying when you spend a lot of time hitting the "-" button and get a not very elegant result.
    regards
    vishal sodani
    Ranch Hand

    Joined: Aug 26, 2000
    Posts: 127
    any open source projects implemented in MVC?so that one can examine how they have done it
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    Try Appache Struts, Tapestry ( i think it is now Apache/Jakarta's ) . The Expresso Framework.
    Note these are frameworks so you can have MVCs all over the place , not just GUIs.
    e.g. IMO The View can be seen as the results of some other Model- Controller. For example in the context of an interface to a database, View is the SQLResults,Model are the business components(stored procedures,business objects like entity beans), Controllers are in between the two.( I think session beans and Message driven beans can be considered a controller to the JMS interface IMO).
    The actual database will be another MVC. These MVCs all sit on top of each other like building blocks. It's a great way of connecting different technologies.
    Some find that the FA´┐ŻADE and MEMENTO patterns could also be used for more stable implementations.
    regards
    [ July 01, 2003: Message edited by: HS Thomas ]
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    We haven't even started to talk about the static relationship between these things, only the dynamic!

    Could you elaborate, Stan, please ?
    I find I cannot distinguish that which is static from the dynamic.
    IMO, static is descriptive - eg DDL, XML descriptors.
    dynamic is that which transforms - eg. process, state changes.
    And you have static typing vs dynamic typing.
    How does this apply to MVC ?
    Are you saying you can have static MVC as well as dynamic MVC ?
    regards
    [ July 14, 2003: Message edited by: HS Thomas ]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    I just meant we showed message flows between M-V-C but not the dependencies on a static class model. The only one I can see being very interesting is "View depends on Model" and not "Model depends on View."
    How does Model send "I've Been Changed" messages to View without establishing a dependency? Model owns the definition of the Interface which View must implement to be notified of changes. I've promoted Model to a package rather than a class. It's actually an architectural layer, so that's fair. Model can operate with one view or another, no views or many views at once.
    I chose "View depends on Model" to support many simulataneous views over a stable model. Can you imagine an application that requires the opposite direction: pluggable Models under the same View?
    We might put the pub-sub code in its own package so that Model and View both depend on Publisher, and neither depends on the other. When is that worth the effort?
    [ July 15, 2003: Message edited by: Stan James ]
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    Can you imagine an application that requires the opposite direction: pluggable Models under the same View?
    We might put the pub-sub code in its own package so that Model and View both depend on Publisher, and neither depends on the other. When is that worth the effort?

  • When flexibility is required use pluggable models,views and controllers. So each should not really reference the other.
  • When ease of distribution is required. e.g. to split the division of labour to suit different skill roles or to increase re-usability.
    Enterprise beans(Model)

  • Javabeans & JSPs(View)
    Session beans & Controller classes(Controller)
    For example, Enterprise beans are pluggable under the same view across applications so that many users see the same data; in addition to being able to have multiple types of views of the same data.

    Was that what you were thinking of, Stan ?
    regards
    [ July 17, 2003: Message edited by: HS Thomas ]
    Stan James
    (instanceof Sidekick)
    Ranch Hand

    Joined: Jan 29, 2003
    Posts: 8791
    I was kinda fishing for something with a view that needs to operate over different models.
    Making something up out of thin air ... maybe a Monitor that could "view" different different system "models" to see how they're running. If you wanted to market your system as "Monitor Compliant" you'd implement the Monitor API and have a dependency on Monitor. A customer could run one Monitor against any number of compliant applications at his site. Zat make any sense? Doesn't seem like an ideal example - Monitor is probably a full blown MVC on its own isn't it.
    Are there better examples in Office? Word can view many types of documents - text, RTF, Word version x,y,z. But I guess they did that by making Word dependent on many models, not many models dependent on Word! What if Word were architected so you could make a new document type that implements a Word model interface so Word could view it without modification?
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    How about a View that constructs an object (to view) from several other objects.
    For e.g. drawing a double helix to do some DNA-analysis. The model objects are two strands with the two interlocking halves attached to each strand. (I believe some of the interlocking bits can go missing in faulty DNA).
    I think you'd say that was really one model!
    But the constructing / deconstructing view is plausible if you'd consider the model objects to be a 2D object and a 3D object that would do some projections off the 2D object.
    Now that's two models if you don't mind my saying so and one view.

    The model owns the interface it knows about , 2D or 3D , but each on it's own doesn't determine the final View.
    regards
    [ July 18, 2003: Message edited by: HS Thomas ]
    HS Thomas
    Ranch Hand

    Joined: May 15, 2002
    Posts: 3404
    What if Word were architected so you could make a new document type that implements a Word model interface so Word could view it without modification?

    This would fit the scenario , too ? But here you have a single interface that View implements.
    Im my example View must implement two interfaces.
    regards
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: MVC - How Did You Do It?
     
    Similar Threads
    designing for multiple layers
    How do I apply Model-View-Controller in Java Swing?
    Model View Controller Question...
    Help : Window->Controller->Business Objects in Java
    MVC & Swing - How model classes work together ?