I'm using a variant of the MVC pattern. In my GUI code, often the need arises to synchronize "view data" (e.g., selected item) between different views.

For example, let's imagine a vector drawing program. We have two views: the image, and a listview of all objects (rectangles, squares, ...). The currently selected item should remain in sync - if you click on "Rectangle A" in the listview, the same rectangle should be highlighted in the image view.

The way I usually do it is to have a ViewState class contained in my model. Is that a good idea? If not, what would be a better solution?

class VectorDrawing
  List<Object> Items;
  DrawingViewstate Viewstate;
class DrawingViewstate
  Object SelectedItem;
  event SelectedItemChanged;
class ListviewController
  ListviewController(VectorDrawing model)
    model.Viewstate.SelectedItemChanged += ... // Subscribe to event
class ImageViewController
  ... // similar to the ListviewController

2 Answers 2


The reason this apparent violation of the MVC separation of concerns isn't really that, is that in your case, this ViewState object isn't, in fact, a part of the View data. It's a part of the model. Just like "the list of objects" is part of your data, so is "the currently selected object".

The View part of this selected-object data might be a highlighted line in a listbox, or a change of color for the selected shape on the canvas, but the fact that the identity of the currently selected item is used in multiple places, and is not just an artifact of the UI process (for instance, selection in a listbox as a way to enable the "delete object" command, for instance) is a good hint that it is, indeed, a part of your model.


I think you are on the right track.

Consider using events to notify the views that a selection has been made/changed. Each view can register a selection listener with the model and then when the selection changes the model can notify the views that the selection has changed.

Decouple a view requesting that an object is selected from the notification that is has been selected.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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