K
K
Kirill Batalin2016-03-07 21:20:02
Java
Kirill Batalin, 2016-03-07 21:20:02

How to notify the controller about user actions?

Decided to write a simple application using MVC. The connection between the model and the view is pretty clear: the model is the subject, the viewers are the listeners. When the data is updated, the model notifies all listeners. The view, when it receives this data, is redrawn based on it.
What is the relationship between view and controller? How should the controller know about button clicks or mouse movement?

After reading the articles, I came to the conclusion that the view should store a link to the controller inside itself and, if necessary, delegate tasks to the controller methods.
For example, in the view we create a button and an inner class that will be a listener for this button. In the inner class, in the actionPerformed method, we call a specific controller method. That is, the "Strategy" pattern is used: the view delegates some of its responsibilities to the strategy-controller.
Did I understand everything correctly? If so, who chooses which viewer to provide which controller? Or is the controller itself doing this, passing itself as an argument to the view constructor?

-----------------
Second question: Can an application have multiple controllers? If yes, in what cases? How do they switch and exchange data between them?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question