There is no MVC standard since there are many implementations. Here is one interpretation of MVC that has been taught in many textbooks:
The definition of the controller in this interpretation is that it processes events from the view, so the view must use the controller.
In the standard MVC model contains and provides data, the controller manipulates the model and receives events from the view, and the view represents the model and generates events for the controller.
MVC is considered a transactional system where a transaction is triggered by an event. Typically, transactions look like this:
- An event is created in the view (for example, a click on a button).
- Event information is passed from the view to the controller.
- The controller calls methods on the model to change it (setters and other manipulation methods that can update some database).
These first steps are the VC link and the MC link. VC exists because events are passed from the view to the controller that needs to be processed, rather than directly processing the view. The MC link exists because the model is updated by the controller in accordance with the event that was triggered.
Hence two ways. First:
- The transaction is completed.
- Separately, the model fires its own events to indicate that it has changed.
- The view listens to the model and accepts the event, and also updates its representation of the model to reflect the changes.
This first path is one interpretation of the MV reference. The MV link is 1) a view that receives information from the model for its data, and 2) a model that informs the update, since it has been changed.
The second way is just one step: after the controller has processed the event, the view is immediately updated, simply updating all its interface elements. This interpretation of the MV connection is that the model simply provides its information for presentation, the same as point No. 1 from the MV line in the first path above.
Here are some of your code modified for the MVC architecture that I described:
public class MyView implements View, ModelListener { private Button myButton; private Controller controller; public MyView(Controller controller, Model model) { myButton = new Button(); myButton.setOnButtonClickListener(new ButtonClickListener() { @Override public void onClick() { controller.onRegisterButtonClick(); } }); this.controller = controller; model.addModelListener(this); } public void setRegisterButtonText(String text) { myButton.setText(text); } public void modelUpdated(Model model) {
And the controller:
public MyController implements Controller { private Model model; private View view; public MyController(Model model) { this.model = model; this.view = new MyView(this, model); } private void manipulateModel() { model.doSomeActions(); } public void onRegisterButtonClick() { maniuplateModel(); } }
Then the model:
public class MyModel implements Model { private List<ModelListener> modelListeners = new ArrayList<ModelListener>(); public void addModelListener(ModelListener ml) { if (!modelListeners.contains(ml)) { modelListeners.add(ml); } } public void removeModelListener(ModelListener ml) { modelListeners.remove(ml); } public void doSomeActions() {
ModelListener pretty simple:
public interface ModelListener { void modelUpdated(Model model); }
This is just one interpretation. If you need further isolation between the various parts, you should study the Presentation, Abstraction, Control (PAC) template. It is more decoupled than MVC, and great for distributed systems. It overwhelms simple web apps, mobile desktop apps, but some client and server apps and most cloud apps can benefit from this approach.
In PAC, you have three parts, representation, abstraction, and control, but abstraction and representation (model and representation) do not interact with each other. Instead, information passes only in the control module and leaves it. In addition, you can have several PAC submodules that interact with each other only through their controls, providing yourself with a good template for distributed systems. In principle, the control module is the main center of any data transfer.
Essentially, your interpretation of MVC may differ from mine or yours. The important thing is that you choose an architectural pattern and follow it to support your code in the future. And you are right that there are ways to separate MVC further. In fact, your example is a bit like a PAC, but instead of removing the VC link, it removes the MV link.
In any case, follow the architecture, document your architecture (so that people know what your interpretation is) and do not deviate from it.