The story begins

MVC is model-view-controller which is model-view-controller. It was first proposed in 1970 by TrygveReenskaug on smalltalk-80 systems.

SmallTalk is widely recognized as the second object-oriented programming language and the first true integrated development environment (IDE) in history.

  • Let’s draw an illustration of how MVC works




Paste_Image.png

The little red boxes in the figure are the WORKING patterns of MVC

  • It can be seen from the figure that the user sends instructions to the View, and the View directly asks Modle to change the state
  • Users can also directly send instructions to the Controller, and then the Controller sends instructions to the Model to change the state of the View through the Model
  • Therefore, the connection between MVC framework mode View and Model is still quite close, and the coupling degree is still very high. Later maintenance changes to View need to change the content in the Model.

MVC is a framework pattern rather than a design pattern and has the following advantages:

  • It’s easy to understand, low tech, and easy to maintain and modify for development and maintenance
  • The coupling degree is not high, the performance layer and the business layer are separated, and each plays its own role.

Its disadvantages are:

  • It’s not very well defined, and it’s not easy to fully understand the MVC pattern.
  • Using MVC requires careful planning, because you have a complicated internals
  • Some small projects using an MVC framework can be more complex

MVC Android pseudo-code implementation

The code in the following picture does not match the data, just for the convenience of understanding how MVC is written in the code, if you need to see the example, you can go to the Internet to know, I am lazy here 🙂

  • The first thing you need is a View, which is an XML layout.




View

  • And then you need a Model which is the data, either the contents of the database, or a collection of lists written in code.




data

  • Finally, you need a Controller, which is an Activity or Fragment

    Access the data in the Model by performing operations in the Controller that the user passes in the View, and then change the state in the View.





    Paste_Image.png

MVP is an evolved version of MVC, which stands for Model-View-Presenter. The emergence of MVP is mainly to solve the problem of high coupling between View and Model in MVC, but also brings good expansibility.

The three roles in MVP mode:

  • Presenter- The broker is a bridge between the View and the Model. The broker gets data from the Model and returns it to the View layer. There is no coupling between the View layer and the Model layer, so the business logic is detached from the View layer.
  • A host Model provides data access. Presenters need to store and retrieve data through the Model layer, which is like a repository.
  • View- A user View is usually an Activity, Fragment, or a View control. It contains a Presenter member variable, and it needs to implement a logical interface that the Presenter calls to return the results to the View element.

Here’s an illustration of how the MVP works





Paste_Image.png

From the graph we can see the biggest difference between MVP and MVC:

  • Complete decoupling between VIew and Model
  • MVP is interface oriented programming, that is to say, the user does not have to know how I implement the specific amount, as long as the user knows this function, directly call.

MVP Android pseudo-code implementation

The code in the following picture does not match the data, just for the convenience of understanding how MVC is written in the code, if you need to see the example, you can go to the Internet to know, I am lazy here 🙂

  • First, you need a Presenter to act as the middleman between the View and the Model




Paste_Image.png

  • And then you need a View and a ViewImpl interface




Paste_Image.png





Paste_Image.png

  • Finally, you need to Model a ModelImpl interface




Paste_Image.png

Why is there no need to decide whether to be MVC or MVP?

The ultimate goal of MVC and MVP is to keep data and UI separate from each other. So how can you do it without having to struggle? Have you heard of object orientation? Listen and then listen to my understanding ~

  • Object – oriented encapsulation, polymorphism, inheritance. Teachers seem to be taught in this way, so in the end is the explanation of polymorphic encapsulation inheritance?
  • Encapsulation encapsulates what the user does not want to see. You can use the object oriented Private property to write the user does not want to see the content inside. For example, on the radio, the user doesn’t need to know the revenue or how to play, it just needs to know that it can play by pressing the button.
  • Polymorphism polymorphism is a variety of manifestations of an object, mainly manifested as: behavior polymorphism and state polymorphism. Behavioral polymorphism is like a father having multiple children, all different, but all with the same father; State polymorphism is like a child having several states throughout the day, including eating, studying, and sleeping.
  • Inheritance? No! I mean objects! A friend of mine told me that inheritance is the biggest misconception about object orientation. Inheritance we can think of as an object that’s made up of a bunch of little objects; For example, the human object is made of hands, feet, head… And other small objects. So inheritance we can not remember, as long as we have a deep understanding of each object can describe the object clearly!

So what do MVC and MVP have to do with object orientation?

First of all, MVC and MVP require that data and UI do not affect each other, so object-oriented is not! ?

  • Object oriented View? The object is the user, which is the View in MC or MVP. If the user needs something, we will show it to them. If the user doesn’t need something, we will encapsulate it and provide a method for you to call.

  • Object-oriented Model? At the same time, object-oriented also needs to subdivide the behavior of the object, such as people can run, can swim… Is this the same Model in MVC that needs to handle different user operations?

  • Object oriented Controller? Finally, object orientation also needs a state to control, such as the human brain. The brain coordinates the balance of the hands and feet.

Learn some English

It is not easy to meet each other in such a big world. It is not easy to meet each other in such a big world. —- February 18th Qiana