1

My View has a textfield and a button. According to MVC pattern on button click should be called a function of the controller. This function should do some operation on the View's textfield content. It is a job of the View to pass the String contained in the textfield or should the controller grab it?

To let you understand here are the two sequence diagram:

enter image description here

Which one best fits MVC?

3 Answers 3

1

Both diagrams only show the V and C of MVC. The missing M is, ironically, the answer to your question:


Data is exchanged between a View and a Controller using a Model


The DATA itself is at the center of everything. The responsibility of each element and the relationship between the elements of MVC is determined by each elements relationship with the data:

  • Model - contains the data
  • View - displays the data
  • Controller - manages the data
0

Transient data should be passed with the event itself. This would essentially be the delta to be applied to a hypothetical model.

Extraneous data would need to be queried as needed by the controller.

Also this isn't MVC. MVC is a small talk pattern. The controller is a single literal function specialised to handle a single event type and is passed the view-model, data-model, and transient event data.

I think this is closer to an MVP (Model View Presenter). Here the Presenter is capable of managing a number of event types in order to present an aggregate behaviour. The same advice applies though. Events are passed event related transient data. Although now it makes little sense to pass the view and model along too, the presenter is already well aware of that information.

2
  • So which sequence diagram makes more sense to you?
    – Diablo3000
    Apr 14, 2020 at 9:04
  • Again context matters here. A Mouse Movement event would crucially need to know which mouse, and the mouse co-ordinates of from and to locations (seen as they change frequently). It certainly isn't important to pass along the colour of the control to understand the event, therefor this would be better looked up from the controller. On the otherhand a colour change event would be sent with the colours (from and to), but lack details such as mouse and mouse co-ordinates, if required the controller would need to request these.
    – Kain0_0
    Apr 15, 2020 at 0:49
0

Nobody outside your view should ever know about text fields, buttons and so on. Instead outside code would know about some text, and the view would know how to display it. Instead of telling the outside of a button, the view should tell the outside that the user wants some action to happen.

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.