Multiple Views to a Model

Discussion in 'Java' started by Jason Cavett, Dec 11, 2008.

  1. Jason Cavett

    Jason Cavett Guest

    I was wondering if anybody here has implemented multiple (Swing) views
    to a common model.

    I'm currently doing this, and I'm having an issue where

    1. I update the model via the first view (type some text into a text
    box)
    2. a listener on the text box sets the data in the model
    3. the model fires a notification (per Observer pattern) because its
    data has changed and
    4. the second view is updated.

    However, when the fields in the second view are updated, THEIR own
    listeners fire off and the underlying model is set again, and proceeds
    to repeat the above sequence (but flipped for the views) so the GUI
    falls into an infinite loop.

    Has anybody ever run into this issue before? How did you handle it?
    (For now, I have a flag checking to see if the notification should be
    fired or not, but it's not very robust or extendable and is causing
    its own set of problems.)

    I'm open to suggestions. Thanks!
     
    Jason Cavett, Dec 11, 2008
    #1
    1. Advertising

  2. Jason Cavett

    Mark Space Guest

    Jason Cavett wrote:
    > I was wondering if anybody here has implemented multiple (Swing) views
    > to a common model.


    It seems to me you need two sets of update methods. One set faces the
    user, and fires update messages when the user updates. The second set
    faces the controller/model, and takes updates, but does not fire change
    events.

    That way user actions cause updates to all views, but changes to the
    model by itself does not. Seems the only way to break loop.


    Customer -> UpdateV -> ViewModel -> fires change notification
    does not fire listeners <- ViewModel <- UpdateM <- Model/Controller

    I'd be interested to hear if anyone has some specific suggestions, I've
    never tackled this exact problem either.
     
    Mark Space, Dec 11, 2008
    #2
    1. Advertising

  3. Jason Cavett

    Stefan Ram Guest

    Jason Cavett <> writes:
    >However, when the fields in the second view are updated, THEIR own
    >listeners fire off and the underlying model is set again


    There should be two different methods:

    - The /model/ change listener method of the second view
    reads from the model and writes to the view. It does
    /not/ fire.

    - The /user edit/ change listener method of the second
    view /does/ fire.

    Possibly, it might help to use two different verbs for
    these two different means of modification, like:

    - »modify_field()« and

    - »change_field()« or so.
     
    Stefan Ram, Dec 11, 2008
    #3
  4. In article
    <>,
    Jason Cavett <> wrote:

    > I was wondering if anybody here has implemented multiple (Swing) views
    > to a common model.
    >
    > I'm currently doing this, and I'm having an issue where
    >
    > 1. I update the model via the first view (type some text into a text
    > box)
    > 2. a listener on the text box sets the data in the model
    > 3. the model fires a notification (per Observer pattern) because its
    > data has changed and
    > 4. the second view is updated.
    >
    > However, when the fields in the second view are updated, THEIR own
    > listeners fire off and the underlying model is set again, and proceeds
    > to repeat the above sequence (but flipped for the views) so the GUI
    > falls into an infinite loop.
    >
    > Has anybody ever run into this issue before? How did you handle it?
    > (For now, I have a flag checking to see if the notification should be
    > fired or not, but it's not very robust or extendable and is causing
    > its own set of problems.)
    >
    > I'm open to suggestions. Thanks!


    It sound like you have two views, each trying to be the controller.

    In the Design section of this example,

    <http://robotchase.sourceforge.net/>

    one can see that the single controller instantiates a model and three
    views:

    <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gc
    s/robot/RobotChase.java?revision=42&view=markup>

    As designed, only the controller was permitted to change the model's
    state; the views were "read-only" with respect to the model.

    Later as a convenience, the RCInfo view was delegated authority to
    change the game's state on behalf of the controller in response to
    mouseClicked events:

    <http://robotchase.svn.sourceforge.net/viewvc/robotchase/trunk/src/org/gc
    s/robot/RCInfo.java?revision=34&view=markup>

    It was a simple matter to re-factor and overload the only legitimate
    command in that setting, move(). In effect, if you have more than one
    controller, you have to keep them straight.

    I look forward to your code example.

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Dec 11, 2008
    #4
  5. Jason Cavett

    Eric Sosman Guest

    Jason Cavett wrote:
    > I was wondering if anybody here has implemented multiple (Swing) views
    > to a common model.
    >
    > I'm currently doing this, and I'm having an issue where
    >
    > 1. I update the model via the first view (type some text into a text
    > box)
    > 2. a listener on the text box sets the data in the model
    > 3. the model fires a notification (per Observer pattern) because its
    > data has changed and
    > 4. the second view is updated.
    >
    > However, when the fields in the second view are updated, THEIR own
    > listeners fire off and the underlying model is set again, and proceeds
    > to repeat the above sequence (but flipped for the views) so the GUI
    > falls into an infinite loop.
    >
    > Has anybody ever run into this issue before? How did you handle it?
    > (For now, I have a flag checking to see if the notification should be
    > fired or not, but it's not very robust or extendable and is causing
    > its own set of problems.)
    >
    > I'm open to suggestions. Thanks!


    The model should check whether the change is really a
    change. It should fire change notifications if "Hello"
    changes to "World", but not if it changes (ahem) to "Hello".

    --
    Eric Sosman
    lid
     
    Eric Sosman, Dec 12, 2008
    #5
  6. In article <ghs4kk$vp$>,
    Mark Space <> wrote:

    > Jason Cavett wrote:
    > > I was wondering if anybody here has implemented multiple (Swing)
    > > views to a common model.

    >

    [thoughtful approach]
    >
    > I'd be interested to hear if anyone has some specific suggestions,
    > I've never tackled this exact problem either.


    FWIW, I often refer to this article:

    <http://en.wikipedia.org/wiki/Model-view-controller>

    But I like this picture better:

    <http://java.sun.com/blueprints/patterns/images/mvc-structure-generic.gif>

    In particular, it suggests that a view should read the model but not
    change it; user gestures should go through the controller. Both the OP's
    and my example allow a view to change the model's state directly. It's
    not forbidden, but the model has to account for the change just as the
    controller would have done (IIUC, as you, Stefan and Eric proposed).

    This article details how Swing uses the MVC pattern:

    <http://java.sun.com/products/jfc/tsc/articles/architecture/>

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Dec 12, 2008
    #6
  7. Jason Cavett

    Stefan Ram Guest

    "John B. Matthews" <> writes:
    >In particular, it suggests that a view should read the model but not
    >change it; user gestures should go through the controller.


    In order to discuss finer details, possibly, one needs to
    tell the /application model/ from the /domain model/.

    »The ApplicationModel layer. This layer mediates between
    the various user interface components on a GUI screen and
    translates the messages that they understand into messages
    understood by the objects in the domain model. It is
    responsible for the flow of the application and controls
    navigation from window to window. This layer is often
    partially generated by a window-builder and partially
    coded by the developer.«

    http://www.c2.com/cgi/wiki?FourLayerArchitecture

    One approach might favor a stateless view and controller: All
    the state would be in a model. In this case, possibly a view
    might have some reason to change the application model, but
    never the domain model.
     
    Stefan Ram, Dec 12, 2008
    #7
  8. In article <-berlin.de>,
    -berlin.de (Stefan Ram) wrote:

    > "John B. Matthews" <> writes:
    > >In particular, it suggests that a view should read the model but not
    > >change it; user gestures should go through the controller.

    >
    > In order to discuss finer details, possibly, one needs to
    > tell the /application model/ from the /domain model/.
    >
    > »...«
    >
    > http://www.c2.com/cgi/wiki?FourLayerArchitecture


    An excellent distinction. Thank you. I also enjoyed the nearby
    discussion <http://www.c2.com/cgi/wiki?MvcIsNotObjectOriented>.

    > One approach might favor a stateless view and controller: All
    > the state would be in a model. In this case, possibly a view
    > might have some reason to change the application model, but
    > never the domain model.


    Yes. IIUC, using this example,

    <http://sites.google.com/site/drjohnbmatthews/kineticmodel>,

    the Assembly of Particle(s) (which manipulate vectors) is the
    DomainModel layer, while the ControlPanel (which governs user settings)
    maintains the ApplicationModel layer.

    --
    John B. Matthews
    trashgod at gmail dot com
    http://home.roadrunner.com/~jbmatthews/
     
    John B. Matthews, Dec 12, 2008
    #8
  9. Jason Cavett

    Stefan Ram Guest

    "John B. Matthews" <> writes:
    >> http://www.c2.com/cgi/wiki?FourLayerArchitecture

    >An excellent distinction. Thank you. I also enjoyed the nearby
    >discussion <http://www.c2.com/cgi/wiki?MvcIsNotObjectOriented>.


    I found the above page on FourLayerArchitecture via another
    document that was written by Trygve Reenskaug himself (the man
    usually attributed with the invention of MVC), namely

    http://heim.ifi.uio.no/trygver/2003/javazone-jaoo/MVC_pattern.pdf

    . So, when Trygve Reenskaug mentions it, it seems somewhat
    authoritative (or, at least: relevant) to me.

    I also know of another source, which says that this
    distinction is the traditional way to implement MVC:

    »Traditional MVC implementations have further split
    the Model into Domain Models and Application Models«

    http://www.mimuw.edu.pl/~sl/teaching/00_01/Delfin_EC/Overviews/MVC.htm
     
    Stefan Ram, Dec 12, 2008
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tim Rogers

    Having One Page Produce Multiple Views

    Tim Rogers, Nov 7, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    581
    George Ter-Saakov
    Nov 10, 2003
  2. Ruben
    Replies:
    3
    Views:
    591
    Dave Mandelin
    Mar 27, 2006
  3. Bill Sexton
    Replies:
    1
    Views:
    416
    Bill Sexton
    Jan 26, 2007
  4. Jason Cavett

    Updating Multiple Views

    Jason Cavett, Feb 9, 2007, in forum: Java
    Replies:
    1
    Views:
    457
    Daniel Pitts
    Feb 9, 2007
  5. Trans
    Replies:
    4
    Views:
    139
    Robert Klemme
    Jul 26, 2007
Loading...

Share This Page