Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I am building a GUI to interface an embedded device to a PC host. The GUI provides control over the device parameters and displays some feedback from it. The GUI also has to emulate some of the device functionality and present it to the user.

I have been advised to use a Model-View-Controller pattern, so that if the device does not acknowledge some command, the the user knows about the ineffectiveness of his actions.

Trouble is, I cannot decide which component(s) should cache data. Example: when a value is printed on the screen, the view clearly has to know it. However, the model also has to know it, as it uses it to calculate other values. Furthermore, the controller also has to know it as, for example, the value change could be proportional to a logarithm of the user action.

How do I pull this off properly?

share|improve this question
    
Is there any constraint requiring you to cache the data ? If not, than you shouldn't. Caching is another layer of complexity, and should be used only if the benefits surpasses the cost of using it. –  Machado Jul 5 '13 at 14:02
    
@Machado, I need to cache the data, as communication with the device is expensive. My problem is how to cache it in only one lace, and not in each layer. Which BЈовић addresses quite well. –  Vorac Jul 6 '13 at 8:28

3 Answers 3

up vote 7 down vote accepted

The view has to know it only in a way of how to show the information to the user. It doesn't really know whether it is a weather temperature, some random text, or anything else. It doesn't interpret data in any way.

The model is the one that stores and process data. And it knows whether to enable or disable a control. In your case, you need to cache data in the model.

The controller controls model and view layers. It is the one that can enable or disable a control in the view by using the value from the model. And it sets all the values in the view by just passing them from the model.

share|improve this answer
2  
Exactly. Caching is part of managing data and so is the responsibility of whatever manages data. Which is the model. (If it actually happens in a helper class, that fact should be still hidden behind the model.) –  btilly Jul 5 '13 at 14:57

For stuff like this I often use a helper.

A class which adds some functionallity that will be used by many controllers, like Caching or Logging.

share|improve this answer
    
To clarify, (IMO) caching is undesirable. It introduces move chances for information mismatch. Ideally I would like all the data to be in the model, and from there synchronized with the device. But I have never implemented this pattern before :( –  Vorac Jul 5 '13 at 9:39
    
Maybe you simply don't need a cache? just load the data into the models when required and show them in the views. if you refresh the view, just destroy all models and generate new ones with the refreshed data. –  K.. Jul 5 '13 at 9:48

I would use additional Model (or Collection of those, which is again a Model) for cacheing. Then use event drivent architecture, where my View emits Events representing user interactions, to which my Caching Model would listen. The Model can emit events and so on. That way all components are nicely isolated and only communicate via events. And if Controller needs to do anything, again make it listen to events.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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