Don't worry about MVC or MVP. Listen to me.

Don't worry about MVC or MVP. Listen to me.

The full name of MVC is Model-View-Controller, which was first proposed by Trygve Reenskaug on the Smalltalk-80 system in 1970.

SmallTalk is explained in Baidu Encyclopedia as follows:

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

Let's take a picture to illustrate the working mode of MVC!

The small red box in the picture is the working mode of MVC

  • From the figure, we can see that the user sends instructions to the View, and then the View directly asks the Modle to change its state.
  • Users can also send instructions directly to the Controller, which then sends them to the Model, and then changes the state of the View through the Model.
  • Therefore, the connection between the View and Model in the MVC framework mode is still quite close, and the degree of coupling is still very high. Later maintenance and changes to the View require changes to the content in the Model as well.

MVC is a framework pattern rather than a design pattern

Its advantages are:

  • It is easy to understand, has low technical content, and is easy to maintain and modify for development and maintenance.
  • The coupling degree is not high, the presentation layer and the business layer are separated, and each performs its own function.

The disadvantages are:

  • It is not well defined and it is not easy to fully understand the MVC pattern.
  • Using MVC requires careful planning because its internal principles are complex.
  • Some small projects will be more complicated if they use the MVC framework

Android pseudo code implementation of MVC

The code and data in the following picture do not match. It is just for the convenience of understanding how to write MVC in the code. If you need to see examples, you can go online to learn about it. I am lazy here:)

  • First you need a View view, which is an XML layout.

  • Then you need a Model

That is, data, which can be the content in the database or the List collection written in the code.

  • ***A Controller is required, which is an Activity or Fragment

By performing operations in the Controller that are passed by the user in the View, the data in the model is accessed, and then the state in the View is changed.

MVP is an evolved version of MVC, and its full name is Model-View-Presenter.

The emergence of MVP is mainly to solve the problem of high coupling between View and Model in MVC, while also bringing good scalability.

The three roles of the MVP model:

  • Presenter-mediator

The main actor is the bridge between View and Model. It gets data from Model and returns it to View layer, so there is no coupling between View layer and Model layer, thus extracting business logic from View layer.

  • Model-Homeowner

Model mainly provides data access functions. Presenter needs to store and obtain data through the Model layer. The Model layer is like a warehouse.

  • View-User

View usually refers to Activity, Fragment or a View control. It contains a Presenter member variable and needs to implement a logical interface. The operations on the View are transferred to the Presenter for implementation. The Presenter calls the View logical interface and returns the result to the View element.

Let's take a look at the working mode of MVP.

From the figure we can see the difference between MVP and MVC:

  • Complete decoupling between VIew and Model
  • MVP is interface-oriented programming, which means that users do not need to know how to implement it specifically. As long as they know that this function exists, they can call it directly.

Android pseudocode implementation of MVP

The code and data in the following picture do not match. It is just for the convenience of understanding how to write MVC in the code. If you need to see examples, you can go online to learn about it. I am lazy here:)

  • First, we need a Presenter as the middleman between View and Model.

  • Then you also need a View and ViewImpl interface

  • ***You also need a ModelImpl interface for Model

Why don't you worry about whether it is MVC or MVP?

The ultimate goal of MVC and MVP is to separate data and UI so that they do not affect each other. So how can we do it without any entanglement? Have you heard of object-oriented? If you have heard of it, listen to my understanding~

  • Object-oriented

Encapsulation, polymorphism, inheritance. The teacher seems to have taught us this way, so what does it mean by polymorphism, encapsulation, inheritance?

  • Encapsulation

Encapsulation is to encapsulate things that users don't want to see, which can be used in Private in object-oriented.

Attributes, write the content that users don't want to see in here. For example, for the play function on a radio, the user doesn't need to know how to play the radio, it only needs to know that pressing this button can play.

  • Polymorphism

Polymorphism refers to the multiple forms of an object, mainly manifested as: behavioral polymorphism and state polymorphism.

Behavioral polymorphism is like a father having multiple children, each child is different, but they all have the same father; state polymorphism is like each child having many states in a day, including eating, studying, and sleeping.

  • Inheritance? No! I mean objects!

A friend of mine told me that inheritance is actually the biggest misunderstanding of object-oriented programming. Inheritance can be understood as an object that is composed of multiple small objects; for example, the object of a person is composed of hands, feet, head, etc. Therefore, we don't need to remember inheritance, as long as we have a deep understanding of each object, we can describe the object clearly!

So what is the relationship between MVC, MVP and object-oriented?

First of all, both MVC and MVP require that data and UI do not affect each other, so isn’t object-oriented the same?

  • Object-oriented View?

The object is what we call the user, which is also the View in MC or MVP. We show what the user needs, and if the user doesn't need it, we encapsulate it and provide a method for you to call it. Isn't this very similar to MVC or MVP!

  • Object-oriented Model?

At the same time, object-oriented programming also requires the subdivision of object behaviors. For example, people can run and swim... Is this the same as the Model in MVC, which needs to handle different user operations?

  • Object-oriented Controller?

***Object-oriented programming also requires a state to control, such as the human brain. The brain is used to coordinate the balance of hands and feet.

<<:  iOS grabs HTML, CSS XPath parses data

>>:  Decryption - The mysterious RunLoop

Recommend

Meizu M6 hands-on experience: Smartphones under 1,000 yuan can also perform well

Since the advent of the Meizu brand, the Meizu No...

APP operation strategy: some tips for developers to retain users

According to Master Chan's analysis of the &q...

How to leverage the Apple Store to promote your own App?

In addition to delivering a good product, how can...

To do word-of-mouth marketing well, these 5 "T"s are essential

In today's Internet age where everyone is a s...

The winter of mobile games is only for speculators

The Chinese mobile game industry, which has been ...

How to make products to stimulate users' desire to spread the word?

In the era of social dividends, the reason why th...

When you are confused about operations, you can use it to find a breakthrough!

A friend who does e-commerce asked on WeChat: &qu...