[Unity] Understanding the MVP framework of UI, talking about the framework

[Unity] Understanding the MVP framework of UI, talking about the framework

import framework

What is a framework?
Many courses will refer to the so-called framework. Before entering the internship, I always felt that it was a very big environment that might need certain components or other different things as the basis for building.
After actual contact,In fact, the so-called framework mainly exists as a development specification., its implementation does not necessarily depend on anything.

Why do frameworks exist?
In the actual development of large-scale projects, there are usually dozens of people working together on the project. Especially in the days of fixing bugs, hundreds of revision records may be submitted a day, and there will be dozens of them every day, which is normal. Obviously, everyone has different habits, different language experiences, and different ways of thinking about code, so they will naturally write in a variety of strange ways. Such systems tend to be weakly readable. For people in the project, sometimes it is even impossible to recognize their own code, because others have introduced or eliminated something in a strange way. Not to mention not that many people can write comments, right.

At this time, the importance of a framework will be reflected. It will divide the code according to certain functions, so that some complex structures will be organized in a fixed way, and the terminal will only realize some specific functions anyway, and the readability of the entire project will be qualitatively improved. promote.

Next is the example
We all know that unity has an observer pattern consisting of delegates, events, and actions. However, everyone's habits are different. Some people will organize events in specific places, while others will think of one event as one event. Then different events or codes will be reused in different functional stages, and the project will gradually become a knot, and finally the so-called Shishan code will definitely appear.
Ok, let's stipulate now that each large function will have multiple components, and if events need to be used, create a new fixed event script for it. In the end, although there will be a huge amount of scripts, the readability has increased a lot.
If we classify different functions for each major function and integrate events with some codes with the same characteristics, the number of scripts will be reduced. At the same time, we stipulate a certain structure, and each script is divided according to fixed functions and structures. The code will be extremely readable while reducing the number of scripts.

Briefly talk about the MVP framework

Framework

The so-called MVP framework is Model-View-Presenter.
That is, the model-view-logic layer (not a standard name).
1. Model layer
This layer is mainly responsible for data, such as external binary files, json files, text files, etc. According to the specific needs of the project, it can be divided into Definition to connect with the data in a fixed format, and the Model layer as the data model, and then a data class can be created for it to initialize the model.
2. View layer
This layer is naturally responsible for the view part. The MVP framework is mainly the UI framework, so this part is mainly UI, including pictures, text, and other parts of the page. According to requirements, it is often divided into initialization UI called by Page externally, Window exists as a single page, and Param has multiple consecutive windows.
3. Presenter layer
This layer is simple and simple, but also complicated. The simplicity is that generally there is only the Presenter, and the complexity is that this layer will handle all the logic. For example, the Button event registration of the View layer needs to be called continuously until the Presenter layer performs event logic implementation and registration.
4. Others that may be used
I know the warehouse Regestroy. When the amount of data is large, or other suitable data storage is established, a warehouse is initialized, which is conducive to data access.

Framework process

Generally speaking, when a Page is opened through an external call, there is mainly a Presenter with corresponding functions in the Page. Presenter is responsible for the window initialization of the target function, including generating the target prefab, generating the corresponding model according to the data, and storing it in the warehouse if necessary. Then register events for different components (if any). Create a dispose method and call the dispose method of the component in it. Make the UpdateView method to achieve component reuse without repeated generation. Call UpdateView for each child window in the UpdateView method.
When a component of the View layer is triggered, such as a button or Scroll event, it will be passed to the Presenter layer, and the Presenter layer will logically process the event. If data is needed, it will be retrieved from the warehouse or the data list already held Get it. If this event will open a new page, the Page that generates the new page will be called, and the Page will generate the next Presenter, and then use the current Page as the previous Page. (The Page here is a bit like a linked list. The last one will always be opened. If the Page is closed, the previous Page will be opened. This process is a switch between different functions. If it belongs to the same sub-function of a large function, it must be considered. Is it in the form of Page or in the form of Param)

last thoughts

This MVP framework is actually not particularly complete, because sometimes multi-layer processing is required, and the one-layer Presenter sometimes does not seem to be able to do what it wants. But most of the time, this structure is a good way to deal with it. There is also the View layer. In theory, all logic should be attributed to the Presenter layer, but sometimes some people may be lazy, saying that there is no need to make such a big move for a little small function. There are also such cases, but it is better to put The function is still at the Presenter layer. Although it will be more complicated, the structure of each function is more neat.

Guess you like

Origin blog.csdn.net/weixin_52540105/article/details/129221728