MVC

MVC began to exist in desktop applications. M refers to the business model, V refers to the user interface, and C refers to the controller. The purpose of using MVC is to separate the implementation code of M and V, so that the same application can use different forms of expression.

For example, a batch of statistical data can be represented by bar charts and pie charts respectively. The purpose of C is to ensure the synchronization of M and V. Once M changes, V should be updated synchronously

MVC(Model View Controller) basically divides the application into three parts, the Model View and the Controller.

  • Model
    • Encapsulate data related to the application’s business logic
    • Implementation of data processing methods
    • Notifying view changes
  • View
    • The view layer is responsible for presenting the data,
    • Connect to the Model layer for data updates
  • Controller
    • Connector between model layer and View layer
    • The part of the application that handles user interaction,
    • Usually the controller is responsible for reading data from the attempt,
    • Control user input
    • Send data to the model

MVC features

The MVC pattern is characterized by separation of concerns, which decouples the data model in the application from the business and presentation logic. In client development, it is to separate and loosely coupled the code between model and View, making it easier to develop, maintain and test the client application

  1. View sends instructions to Controller;
  2. After the Controller completes the business logic, it asks the Model to change state
  3. The Model sends the new data to the View, and the user gets feedback
MVC advantages
  • Low coupling and separation of the view layer from the business layer allows changes to the view layer code without recompiling the model and controller code
  • High reusability
  • Low lifecycle cost
  • MVC takes the art out of developing and maintaining user interfaces
  • High maintainability and separation of the view layer from the business logic layer also make Web applications easier to maintain and modify
  • The deployment of fast
MVC shortcomings
  • Not suitable for small, medium sized applications, and spending a lot of time applying MVC to applications that are not very large will not pay off
  • Views and controllers are separate but tightly connected parts. Views without controllers have limited applications, and vice versa, which impedes their independent reuse.
  • Inefficient access to model data by views, which may require multiple calls to get enough display data, depending on the model’s operation interface. Frequent and unnecessary access to unchanged data can also hurt operational performance
The MVC application:

At the beginning of the popularity of web apps, MVC was used in Java (struts2) and C# (ASP.NET) server applications, and later in client applications, AngularJS was born based on the MVC pattern.

MVP

MVP (Model-View-Presenter) is a modified Model of MVC proposed by Taligent, a subsidiary of IBM. The same as MVC: Controller/Presenter handles business logic, Model handles data, and View handles presentation. Just change the name of Controller to Presenter and change the communication direction.

The characteristics of the MVP
  • The View and model do not communicate with each other. The View and model communicate only with the Presenter, which is completely decoupled from the View and model. The main application logic is in the Presenter
  • Views are very thin and don’t deploy any business logic, called passive views,
  • A Presenter is not directly associated with a specific view, but interacts with a defined interface that keeps the Presenter unchanged when changing the view, so it can be reused. In addition, you can also write a test view that simulates the actions of the user.
MVP vs. MVC
  • MVP: Views don’t use the Model directly, they communicate with each other through the Presenter, and all interaction happens inside the Presenter
  • MVC: The View reads directly from the Model rather than through the Controller
MVP advantages
  1. The model is completely separate from the view, and we can modify the view without affecting the model
  2. The model can be used more efficiently because all interaction takes place in one place — the Presenter
  3. We can use a Presenter for multiple views without changing the Presenter logic. This feature is useful because the view changes more frequently than the model
  4. If we put logic in Presenter, we can test it out of the user interface
MVP shortcomings

Views and presenters interact too often, making them too closely related. In other words, as soon as the view changes, the present changes

MVVM

MVVM(Model-view-ViewMode) If MVP is a further improvement of MVC, then MVVM is a complete change of mind. It is the idea of “data model data bidirectional binding” as the core.

Therefore, there is no connection between the View and the Model, and the interaction between the Model and the ViewModel is two-way, so changes in the data of the View will also modify the data source, and changes in the data source will be immediately reflected in the View.

The MVVM advantages:

The MVVM pattern is similar to the MVC pattern. Its main purpose is to separate the View from the Model. It has several advantages:

  1. Low coupling, views can be changed and modified independently of Model, a ViewModel can be bound to different “views”, when the View changes, the Model can not change, when the Model changes, the View can not change.

  2. Reusability: You can put some view logic in a ViewModel and have many views reuse that view logic.

  3. Independent development, where developers can focus on business logic and data development (ViewModel) and designers can focus on page design, Expression Blend makes it easy to design interfaces and generate XML code.

  4. Testable. Interfaces have always been harder to test, and now tests can be written against viewModels.

Typical applications of this are. NET WPF, JS framework Knockout, AngularJS, etc.