MVC Pattern
The Model-View-Controller (MVC) is an architectural pattern that separates an application into three main logical components: the model, the view, and the controller. This is done to separate internal representations of information from the ways information is presented to and accepted from the user. Traditionally used for Desktop GUI apps, the pattern became popular with the advent of web apps. Today almost all popular languages support this architecture.
Components
- The model is the central component of the pattern. It is the application’s dynamic data structure, independent of the user interface. It corresponds to all the data-related logic that the user works with. It directly manages the data, logic and rules of the application.
- A view can be any output representation of information, such as a chart or a diagram. Multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.
- The third part or section, the controller, accepts input and converts it to commands for the model or view. It acts as an interface between Model and View components to process all the business logic and incoming requests, manipulates data using the Model component and interacts with the Views to render the final output.
Interactions
In addition to dividing the application into three kinds of components, the model–view–controller design defines the interactions between them.
- The model is responsible for managing the data of the application. It receives user input from the controller.
- The view means the presentation of the model in a particular format.
- The controller responds to the user input and performs interactions on the data model objects. The controller receives the input, optionally validates it and then passes the input to the model.
These interactions can be aptly represented by the following flow diagram:
Why MVC?
The following advantages of the MVC design pattern make it suitable to be used in software development:
- Simultaneous development — Multiple developers can work simultaneously on the model, controller and views.
- High cohesion — MVC enables logical grouping of related actions on a controller together. The views for a specific model are also grouped together.
- Low coupling — The very nature of the MVC framework is such that there is low coupling among models, views or controllers
- Ease of modification — Because of the separation of responsibilities, future development or modification is easier
- Multiple views for a model — Models can have multiple views