Let's take a display-free program to calculate Fibonacci numbers. In the MVC paradigm, this would be the model.

Now as long as you only want to push in an input, and get out an output, then the view is fairly simple... you just come up with a way to display the output and maybe the input and you're done.

But supposed you wanted to write something which showed the state of the computation at each step. Then the model and the view are more intertwined and the model can no longer act as if it "just does it's job" regardless of what view gets tacked onto it later.

So, can anyone contribute some more examples supporting or defeating my idea that sometimes it is not possible to work in a decoupled fashion?

A related node of mine in which I posed a similar quandry is here.

  • Comment on How realistic is separation of computation from it's display?

Replies are listed 'Best First'.
Re: How realistic is separation of computation from it's display?
by dws (Chancellor) on Sep 01, 2001 at 22:56 UTC
    But supposed you wanted to write something which showed the state of the computation at each step. Then the model and the view are more intertwined and the model can no longer act as if it "just does it's job" regardless of what view gets tacked onto it later.

    Turn the computation data model into something that can be "stepped". That is, instead of   $computation->run(); set it up so that you can do something like

    while ( ! $computation->done() ) { $computation->step() }
    Assuming the computation data model is playing fair, it'll notify all views that it has changed after each step, giving each view a chance to redraw (or reconfigure itself and redraw). You're going to take a big performance hit, but if the point is to illustrate the computation, so be it.

    This is a standard technique in the MVC world.

    The process of thinking through how to split a computation into steps, and what state needs to be maintained between steps, can be difficult, but is often be very insightful.

Re: How realistic is separation of computation from it's display?
by Masem (Monsignor) on Sep 02, 2001 at 01:28 UTC
    In the MVC structure, you absolutely need to have some OOP-based Event structure; Java's Actions/Listeners is a good example, but there are others that better or worse in some situations.

    In Java, you have components with Actions (such as PressedAction for a UI button); you have other components that are Listeners that you must explicit connect to those actions. When the action is triggered, each connected Listener is told of this and they can take any necessary steps if they need do. In the above model, for the dynamic display during processing, you'd trigger an "InProgressAction", which then any InProgressListeners, including your progress display, respond to; the process display can be anything that responds to such; maybe as simple as a LED-style light that toggles state on each triggered action; maybe a status line that prints a text-bit that is sent, maybe a status bar that fills to 100%. You could also probably make a more specific FibonaciProgressIndicator that does something more interesting in your case. Since the triggered Actions can send out derivable events, you can overload these with extra that that you might want FibonaciProgressIndicator to respond to, but 'standard' ProgressIndicators have no idea what to do with. So unless you want speciality from the basic classes, there's no need to have strong 'interfaces' between your progress display and the underlying model.

    (Another possiblity is a more general Broadcasters/Subscribers model, in which only one event engine class exists, which all broadcasters and subscribers are registered to; when a broadcaster sends out an event, the engine class sends it to subscribers which decide if they want to do anything with it. You can do some further optimization by having registeration flags to indicate which event to look for by subscribers, or what type of events will come in from Broadcasters, but this is a good type to use when you cannot completely encapsulate the event tree for a given operation into a single set of MVC classes. )

    -----------------------------------------------------
    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
    It's not what you know, but knowing how to find it if you don't know that's important

Re: How realistic is separation of computation from it's display?
by Zaxo (Archbishop) on Sep 01, 2001 at 23:11 UTC

    A counterexample: trace programs and debuggers manage to look inside a running program which was not prepared for them. Techniques vary, but usually involve setting interrupts or acting as an alternate environment.

    There is coupling of course, provided by POSIX or the win32 API. The key is that coupling is restricted to well-defined interfaces.

    After Compline,
    Zaxo

Re (tilly) 1: How realistic is separation of computation from it's display?
by tilly (Archbishop) on Sep 03, 2001 at 05:52 UTC
    Modularity usually does not cooperate very well with attempting to break encapsulation. That is usually a Good Thing. (And most of the time when people want to break encapsulation, that is a Bad Thing.)

    For instance take your example above. You have a nicely encapsulated function to produce Fibonacci numbers. Perhaps it looks like this:

    sub fib { my $n = shift; if ($n == 0 or $n == 1) { return 1; } else { return fib($n-1) + fib($n-2); } }
    So you want to break the encapsulation to show how the computation works. Well what happens if someone wants to later on Memoize the function so that it will calculate answers at a reasonable rate? There is no good way to show the logic and also short-circuit uselessly repeated logic. So you are now stuck with an exponentially bad algorithm to calculate this function because someone might want to display it...

    OK, this is a toy example, and with excess work you can work around it. But the principle still holds. When you break the encapsulation, you cause extra work and limit your ability to optimize under the hood. And this is a bad thing. By contrast if you separate display and implementation, then you don't needlessly complicate your life later. Sure it makes some things harder now, but if you can hold that line it will make a lot of them easier later.

Re: How realistic is separation of computation from it's display?
by clemburg (Curate) on Sep 03, 2001 at 13:19 UTC

    But supposed you wanted to write something which showed the state of the computation at each step. Then the model and the view are more intertwined and the model can no longer act as if it "just does it's job" regardless of what view gets tacked onto it later.

    In that case, the "state of computation at each step" would be regarded part of the model in the MVC paradigm.

    Christian Lemburg
    Brainbench MVP for Perl
    http://www.brainbench.com

Re: How realistic is separation of computation from it's display?
by hsmyers (Canon) on Sep 03, 2001 at 12:04 UTC
    It occurs to me that you might be able to have your cake and eat it (or view it) as well. If you leave the original module alone and simply redirect it's output, you can then manipulate the result in a way to simulate actual operation. You might have to tweak the output to increase the detail a smidge, but that doesn't strike me as any sort of integrity violation.

    Any way you look at it, there is an enormous difference between production and process, hard to write clean code to do both. Self referent code is usually not do-able in as clean a model as you seem to want.

    hsm

Re: How realistic is separation of computation from it's display?
by Brovnik (Hermit) on Sep 17, 2001 at 15:54 UTC
    I came across exactly this issue recently when I was starting to write a simulator. It has essentially 3 parts.
    • Computation - calculation of changes for each iteration
    • Setup & control - input data on what is being modelled and drive the simluation
    • Graphics - Representation of data

    I was particularly looking for the ability to strip out/replace either the computation (to allow a different model to be simulated), or a different Graphics package (in case the original isn't high performance enough).

    I spent a long time deciding what graphical system to use, and settled on Gtk+ for the display and Glade for the visual UI building.

    The computation model was written as a separate module.

    This combination allowed almost complete separation between the computation, the control and the grpahics, with just a few object references passed between the modules, allowing easy replacement of one of the modules.

    I plan to publicise the code when it is a bit more advanced.
    --
    Brovnik