The Single Responsibility Principle states that "a class should have one reason the change".
In the MVC pattern, the Controller's job is to mediate between the View and the Model. It offers an interface for the View to report actions made by the user on the GUI (e.g. allowing the View to call controller.specificButtonPressed()
), and is able to call the appropriate methods on the Model in order to manipulate it's data or invoke it's operations (e.g. model.doSomething()
).
This means that:
- The Controller needs to know about the GUI, in order to offer the View a suitable interface to report user actions.
- It also needs to know about the logic in the Model, in order to be able to invoke the appropriate methods on the Model.
That means that is has two reasons to change: a change in the GUI, and a change in the buisness logic.
If the GUI changes, e.g. a new button is added, the Controller might need to add a new method to allow the View to report a user press on this button.
And if the business logic in the Model changes, the Controller might have to change in order to invoke the correct methods on the Model.
Therefore, the Controller has two possible reasons to change. Does it break SRP?