2

I’m developing a Matlab GUI for a scientific computing application and need to plot fairly heavy intermediate results.

Currently, the computation is represented as a function. The GUI accepts user input and passes it to the computation function, which returns the final data results.

Architecturally, the GUI incorporates a view and controller class. Main initializes these objects, in addition to defining the computation function, which lives in a sourced file.

Now, because I need to plot intermediate results, I’m trying to figure out the best way to create separation of concerns between the computation logic and plotting. The physical display of intermediate results to the user does not need to take place until the computations are completed.

My instincts are that the computation functions should not know that they’re being plotted.

Ideas so far:

  1. Computation logic accepts UI axes handles as an input argument, plotting takes place inside the computation logic. This appears to violate separation of concerns.
  2. Computation logic returns a data structure containing intermediate results. The GUI is responsible for binding the data to GUI plots. I don’t like this, as it seems cumbersome to keep all the intermediate data around.
  3. I create a plotter object, which is initialized in main() and can be configured (e.g., turned on or off). During the computation procedure, the computation function calls a plotter method that generates the needed plots and discards the data. The plotting logic in the computation function is reduced to a single plotter method call, but it still exists. This is analogous to idiomatic solutions for logging in large applications.
  4. The computation now lives inside a class, with intermediate results as a continuously overwritten member variable. The completion of an intermediate result triggers an event, a plotter object listens for that event and accesses/plots the computation class’s intermediate result member variable. This seems most fully separated, as now the computation logic has no knowledge in its code to being plotted.

Ultimately, the computation is generating and discarding intermediate results that need to be captured and processed by another object, and I need some way of interspersing additional logic without modifying the original function.

Any other ideas? This is being done in Matlab, but if there’s a conventional way of tackling this in other languages, I’d still love to learn about it.

Past Research:

This problem appears very analogous to organizing logging code, as in this question. However, while it seems widely accepted that logging statements intersperse business logic, it’s unclear if plotting demands stronger separation, or if inseparably it should itself be considered business logic and contained within the computation code.

1
  • Can you save the intermediate results as a series of values (or points) and plot that at the end? Two interacting components are decoupled via a suitable abstraction; the abstraction here is that a plot is just a graphic based on a series of input data points (even with nominally continuous functions) - I'm guessing you can treat it that way in matlab. However, if it's preferable to do it "on the fly", then dependency injection as described by Doc Brown is a good option. – Filip Milovanović Mar 21 '20 at 23:58
3

I would go for solution #5 - standard Dependency Injection:

  • Either at construction time (in case the computation is implemented in a class), or when calling the function, the computation logic takes an instance of some interface for reporting intermediate results. That interface needs to contain methods which can be called at the appropriate places during the computation.

That way, the computation does not know anything about the implementation of that interface (which could be some logger, or a plotting logic, some other kind of GUI, or something completely different). It does not know if and how the intermediate results are processed, displayed immediately, buffered, send somewhere as asynchronous events, or ignored.

If you want the GUI to plot those results, it has to pass an interface implementation which does exactly this. Still the two components stay strictly separated.

4
  • In MATLAB it will be a callback function (function handle). – rwong Mar 21 '20 at 23:53
  • This makes sense. I think my initial hesitation has been my MVC reading saying that business logic should not know about how it's presented, and therefore calculations should not depend on plotting since plotting is part of the view. But, the generation of graphics is so intimately tied to the data and based around business rules that it should be considered part of the business logic, belongs in the module of MVC, and therefore the calculations can depend on its interface. – Dragonsheep Mar 22 '20 at 1:02
  • I think an extreme example of this phenomenon might be a computer graphics engine, where all of the rendering calculations properly belong in the module and not the view. Lightweight plotting and processing likewise belongs on the business side of this app, preparing barebones graphs, which the view may be able to further alter. – Dragonsheep Mar 22 '20 at 1:04
  • @rwong: or it could be an abstract class, AFAIK. – Doc Brown Mar 22 '20 at 10:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.