Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

MVC (Model, View, Controller)

The core of apple’s official recommended architectural pattern (MVC) is to control through the Controller layer. First, take a look at the official MVC schematic diagram, as shown below:

1. Responsibilities of each layer of MVC

1.1, the controller layer

  • generateviewAnd then assemble itview
  • The responseViewThe events and actions ofviewThe agent of
  • To deal withviewLife cycle of
  • Handle jumps between interfaces
  • callmodelData acquisition interface, get back data, processing, rendering toviewAccording to

1.2, the model layer

  • Business logic encapsulation
  • Provides data interfaces tocontrolleruse
  • Data is stored and read persistently
  • Store data as a data model

1.3, the view layer

  • Interface element construction, animation effect, data display
  • Accept user actions and feedback visuals

2, summarize

The user clicks On View- > View response event -> pass event to Controller via proxy -> initiate network request to update Model- >Model finishes processing data -> Proxy or notification to Controller- > Change View style -> Done

3. Advantages and disadvantages

3.1 advantages,

The Controller is used to control the overall situation, while the changes of View and Model are separated, and the complex and chaotic project structure is clearly organized.

3.2 and disadvantages

With the increase of business logic, a large number of logic codes are put into the Controller, resulting in the Controller becoming more and more bloated and high maintenance costs.

MVP (Model, View, Presenter)

1, an overview of the

MVP mode is an evolutionary version of MVC mode, in which the Model is not much different from the Model in MVC mode, and there are some differences between View and View in MCVC mode. The View layer in MVP can be viewController, View and other controls. The Presenter layer acts as an intermediary between the Model and the View, retrieving data from the Model layer and passing it to the View.

The downside of MVC is that it doesn’t distinguish between business logic and business presentation, which is unfriendly to unit testing. MVP optimizes this by separating the business logic from the business presentation as an MVCP.

MVP is shown below:

2, summarize

Compared to the MVC pattern, M and V functions remain the same, the original C is now only responsible for layout, and all the business logic has been moved to the P layer. Once the P layer handles the business logic, if you want to change the view’s display, you can do so through callbacks that reduce coupling and allow you to test the P layer’s business logic separately.

3. Advantages and disadvantages

3.1 advantages,

  • The model and view are completely separate, and you can modify the view without affecting the model
  • More efficient use of models,ViewDo not rely onModel, it can be said thatViewCan achieve complete separation of business

3.2 and disadvantages

In addition to handling business logic, presenters also need to handle the view-Model protocol, which can lead to code bloat.

MVVM (Model, Controller/View, ViewModel)

1, an overview of the

MVVM was developed by Microsoft, and MVVM is actually based on MVP. In MVVM, View and Controller are associated, and we treat them as one component. Neither View nor Controller and can refer directly to model, but to the ViewModel. The place in the Viewmodel that handles business logic, such as user input validation logic, view display logic, network request logic, and so on. This design pattern slightly increases the amount of code, but reduces the complexity of the code.

The schematic diagram of MVVM is as follows:

2, summarize

The biggest improvement of MVVM and MVP over MVC is that P or VM creates an abstraction of the view, pulling the state and behavior out of the view to form a new abstraction. This allows business logic (P/VM) and business presentation (V) to be tested separately and reused for clearer logic structure.

3. Advantages and disadvantages

3.1 advantages,

Views can be changed and modified independently of the Model, and a viewModel can be bound to different views, reducing coupling and increasing reuse.

3.2 and disadvantages

Not suitable for simple projects, large projects with many view states are expensive to build and maintain

Project source code reference: github.com/jhs521mmy/M…