• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

MVC - How Did You Do It?

 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
  •  
    Ranch Hand
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    Ranch Hand
    Posts: 37
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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?
     
    Stan James
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Sheriff
    Posts: 7001
    6
    Eclipse IDE Python C++ Debian Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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.
     
    Stan James
    (instanceof Sidekick)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 ]
     
    author
    Posts: 11962
    5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    I wonder why the kind of graphs like Stan and HS drew aren't included in every pattern book explaining MVC... Good job.
     
    HS Thomas
    Ranch Hand
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    Ranch Hand
    Posts: 127
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    any open source projects implemented in MVC?so that one can examine how they have done it
     
    HS Thomas
    Ranch Hand
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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)
    Posts: 8791
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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
    Posts: 3404
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    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
     
    If you want to look young and thin, hang around old, fat people. Or this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic