MVC vs MVP (Personal understanding only)




Hierarchy of MVC

MVC is divided into: Model (data abstraction), View (view), Controller (controller) three-tier architecture. Next, we will analyze the responsibilities corresponding to each layer one by one.

  • View layer: Corresponds to the xml file in the layout folder in Android. When Activity/Fragment is started, a layout file of R.layout.xxx will be loaded, so that the view we have defined in xml is displayed in the view. view.

  • Controller layer: The corresponding is Activity/Fragment. When the Activity/Fragment loads the layout file, we need to find the corresponding view in the Activity/Fragment by findViewById(int), and set the corresponding properties and listeners for the found view. Before setting the properties of the view, we are likely to request data from the model first, and when the data is called back, the controller will update the view.

  • Model layer: The corresponding objects are some DataSource and DataBean related objects, where DataSource refers to the source of data. There are two main sources of data in general, one is sqlite and the other is webservice, and we are accustomed to encapsulating the sources of these two types of data in one repository. For the caller, only one of the access interfaces in the repository needs to be called. To obtain data, but whether this data comes from memory, sqlite or webservice, we do not know, from protecting the logic of the call implementation, decomposing the related implementation, to the extreme simplicity and conciseness of the caller, and in the unit test Testing the interface is also very convenient.

To make an analogy, Activity Window View

                        window frame (window frame) glass window grille on glass 


Advantages and disadvantages

Advantages:
     The logic is clear, the Controller layer and the View layer are together (in a class, in an Activity or Fragment), the 
     hierarchy is clear       
     and convenient for project testing and later maintenance    

 Disadvantages:
      Controller layer and View are too coupled

      Activtiy class or Fragment class is too bloated


Hierarchy of MVPs

  • View layer: The view layer, which corresponds to not only the xml file in the layout but also the display of Activity/Fragment as the view. Doing so expands the responsibilities of the View layer, which not only sets UI display and properties but also includes lifecycle callbacks.

  • Presenter layer: the presenter layer, which is equivalent to the business logic part in the Controller. It is mainly responsible for the communication between the view and the model layer, responding to the request of the view layer in a timely manner and actively calling the data acquisition of the model layer, and will obtain The result of the data is returned to the view layer. The presenter is a new class created, and the view holds an instance of the presenter from the time it is created. When the view has some request response or the life cycle changes, it will quickly initiate a request to the presenter and let the presenter respond. processing, such as: refreshing data, clearing data to prevent leakage, etc.

  • Model layer: The data abstraction layer model here is the same as the model layer in MVC.

Advantages and disadvantages

Advantages:  clear
     logic, clear hierarchy, and obvious division             of    module responsibilities



     Each view has a presenter, and there are many more classes.


Personal understanding, if I have any mistakes, you can ask me!





Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324520069&siteId=291194637