4

I know we can use the Mediator Pattern to communicate between view models but what about views?

Lets assume I have a button in View A which should trigger functionality of View B. How is this preferably done? I could use the Mediator but it seems more like a hack.

Here is a thought of mine:

enter image description here

4

Lets assume I have a button in View A which should trigger functionality of View B.

According to the MV(V)C Pattern there should not by any "functionality" in any View.

The functionality should be in the controller layer which handles the applications state by altering the model as needed.

Now you can have as many view layers (in parallel or sequentially) viewing the model data and manipulating them via the controller.

Since the model provides infrastructure to propagate changes the View B can register for certain Properties to get change information so that it can update itself when View A changed that property.


So for functionality I have extra classes called (some prefix)ViewController? Do we also have controller for viewmodels? (ViewModelController) – Asperger

Do not put your fath on that names. After all names are just dust in the wind.

The point is that you have to deal with this 3 layers model, controller, and view. The different names (MVC,MVVC, MCP) are only variation and don't mean that much to me.

You also do not necessarily put them into separate projects, packages or classes. The pattern is basically about the direction of communication:

  • the model does never talk to anything else directly, only via interfaces it provides itself (mainly getter/setter methods and *Listener interfaces).
  • the controller only talks to the model via the models interfaces.
  • the view gets data (and change information) to display from the model and translates user input to calls in the controller layer. It never updates the model itself.
  • So for functionality I have extra classes called (some prefix)ViewController? Do we also have controller for viewmodels? (ViewModelController) – Asperger Jul 10 '17 at 13:09
  • I think in this case it would be called MVVMC – Asperger Jul 10 '17 at 13:12
  • @Asperger Your views do not need to communicate at all... if they do, they are no longer views! Views only contain the layout of the UI. Any communication or coordination needs to be done through the controller. – TheCatWhisperer Jul 10 '17 at 13:18
  • @TheCatWhisperer so is it correct to say that my interceptor is actually the controller that acts as a messenger between my lets say viewmodels? Viewmodels arent directly part of the view so that should be fine. – Asperger Jul 10 '17 at 13:20
  • Your views should not be communicating at all. If you went on a tour of an art museum, would you expect the paintings (the views) to be talking to each other or you? The tour guide (the controller) would be the one communicating with you, and bringing you to different exhibits. – TheCatWhisperer Jul 10 '17 at 13:28

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.