Android MVC, MVP and MVVM framework patterns
MVC Development Framework
- View: corresponding to layout files and custom views, responsible for notifying Controller of user requests and updating interface according to model;
- Controller: corresponding to Activity and Fragement, is responsible for processing the business logic, receiving user requests and updating the model. (In fact, our Activity has both MVC3 roles and thousands of lines of code at every turn!)
- Model: Data Model, responsible for the logic related to data processing, encapsulating application state, responding to state queries, and notifying views of changes.
MVP Development Framework
- A View corresponds to an Activity and is responsible for drawing the View and interacting with the user
- The Model is still the data Model, responsible for the logical and physical models associated with data processing
- Presenter is responsible for completing the interaction between the View and Model and handling the business logic
MVP framework advantages: ①Model layer and View layer fully decoupled. ② Complex tasks are broken down into smaller tasks that can be easily solved. The smaller something is, the less buggy it is, the easier it is to debug, and the better it is to test. The View layer in MVP mode will be simple and intuitive. (4) Background tasks are no longer associated with the Activity, which neither causes a memory leak (OOM) nor depends on the Activity rebuilding. (5) The Persnter layer takes care of the business logic, so that the business logic is stripped away from the worry of constantly changing requirements, allowing developers to focus more on solving problems. ⑥ The Persenter layer can be unit tested directly, so that there is no need for implementation and View layer and Model layer. The downside of the MVP framework: It takes a lot of classes to write, and now it takes six classes to do what one class could do!
MVVM development framework
- MVVM is an upgrade to MVP, renaming Presenter to ViewModel. The key is the two-way binding between the ViewModel and the Model. When the View has user input, the ViewModel notifies the Model to update the data. Similarly, when the Model data is updated, the ViewModel notifies the View to update the data.
- The emergence of MVVM is also to solve the decoupling between the View layer and the Model layer, which is fully decoupled.
- MVVM solves much of the MVP’s shortcomings compared to the MVP
- Android Google’s DataBinding Library technology, introduced in 2015, makes bidirectional binding between viewModels and Models much easier. If you want to do something similar on IOS, you can do it using KVO.
The advantages of the MVVM framework ① inherits all the advantages of the MVP framework ② it is easier to write without creating many classes. Make the view layer, business logic layer, data model layer and hierarchy clearer. ③ While fully decoupling the Model layer and the View layer, it also realizes the two-way binding of data and View, and automatically updates the View when the data changes. This feature is very useful and is undoubtedly the best solution for some real-time applications. For example, some apps for stocks. Development tools currently only support Android Stduio, it is obvious that Eclipse has been eliminated by Google. Android Plugin for Gradle 1.3.0 or later
Second, Android componentized development and plug-in development technology
Android component development
What is componentized development? Componentized development is the extraction of separate business modules. Each module is a module. Normally, there can be multiple modules in an App, but generally, only one module is set as Application and the others are set as Library. Componentized development means that each module can be run. Therefore, each module is set to Application during development and merged at release time. 2. Why componentized development? ① When the amount of code in an Android project reaches a certain point, compiling can be painful, ranging from a minute or two to five or six minutes. Component-based development can effectively reduce the coupling degree of code modules, make the code architecture more clear, and modular compilation can effectively reduce the compilation time, of course, the total compilation time will not be reduced, but after the App modularization development of a module, only need to compile a specific module, can quickly compile and debug. (2) Componentization enables the decoupling and reuse of services.
Evolution of the componentized development model
(1) Single project development mode
This kind of development model has clear module partition, and through the logical layered presents a good structure, the model is the most familiar, usually used for the rapid development of the product, early team on a smaller scale, as the product of iteration, the business is more and more complex, then there is the project structure complexity increase, at this time we are faced with several problems:
- The actual business changes very quickly, but the coupling degree of the business module before the project is too high, affecting the whole body.
- Any changes made to the project must compile the entire project
- Functional and system tests are performed every time.
- There are many conflicts in collaborative team development. They had to spend more time communicating and coordinating, and during the development process, no one could focus on their own function points, which affected the development efficiency.
- The project cannot be configured and assembled flexibly. For example, today the product manager said to add this function, tomorrow said to remove, the day after tomorrow to add.
(2) the main engineering multiple component development model on the basis of the “single project” model, each business is extracted from the business layer, encapsulated into the corresponding business component, will be based in the library each part extracted, encapsulated into basic components, and the main engineering is a working app, as the entry of the components (also known as main engineering shell program). These components are presented either as jars or as AArs. The main project uses the functionality provided by components in a dependent manner.
Advantages: ① Each member can focus on the business they are responsible for without affecting other businesses. Meanwhile, with the help of stable basic components, code defects can be greatly reduced, so that the whole team can efficiently promote the development progress in a parallel development way. ② The original business does not need to be tested again, but can focus on the test of the changed business and the final integration test. Cons: So far, we have effectively addressed some of the problems in the “single-project development model”, which is fine for most teams, but there are still some areas where the model can be improved: every time you change a dependency package, you need to recompile to generate a Lib or AAR. For example, Xiao Yan took over a project with more than 40 components. In the final integration of all components, Xiao Yan found that there was a problem in one component. In order to locate and modify the problems in the component, Xiao Yan kept debugging the component. Under this model, components cannot be separated from the main project, which means that after each modification, Xiao Yan has to wait in the long compilation process. To make matters worse, it is now only 5 hours before launch, 10 minutes per compilation, 20 times to fix this bug,…. You don’t have to do anything. You can file your exit papers
(3) Development model of main project and multiple sub-projects Is not hard to find, this kind of development model on the structure and the “main engineering components more” is no different, the only difference is that all of the business components are no longer mouble but as a child project, basic component can make the moudle, can also be a sub project, which project and main projects: the Debug mode and down as the app, can separate the development of operation , commissioning; In Release mode, as Library, it is relied on by the main project and provides services to the main project.
Advantages: ① Under this model, what will Xiao Yan do when he finds defects in a certain business component? For example, if there is a problem with basic component 2, in Debug mode, basic component 2 can run independently as an APP. Therefore, it is easy to modify and Debug the module separately. You only need to recompile the entire project once the final changes are made. ② It is not difficult to find that this development model can effectively reduce The Times of full compilation, reduce the compilation time, and facilitate developers to develop debugging. ③ For test students, functional test can be carried out in advance, and timely participate in the development process, to minimize the risk.
Android plug-in development
Plug-in and component-based development are slightly unnecessary. In plug-in development, the whole APP is divided into many modules, including a host and multiple plug-ins, and each module is an APK or Dex package (each component-based module is a lib, although it is an application when debugging, it is still treated as a lib when packaging). The host APK and plug-in APK should be packaged separately or in combination.
1. Why is the plug-in development model developed? We know that an APK package has only one classes.dex file, but the maximum number of methods in the dex file is 65535. If the business logic of an application is very large, such as Taobao, JINGdong. Obviously, an APK file is not enough. So what are the solutions? There are the following:
- Replace some logic with H5, React Native, and Flutter
- Delete useless code
- Buy the paid version of Proguard
- Another is the plug-in pattern.
2. Advantages of plug-in
- The module of decoupling
- Applications can be updated dynamically (hot update, differential pack update)
- Dynamically fix online application bugs without having to rebuild the version and upgrade again.
- Efficient parallel development
- Load corresponding modules on demand, lower memory usage
- Save traffic upgrade
3. Basic knowledge of Android plug-in
- Android Interprocess Communication (IPC) Binder mechanism
- APP packaging process
- APP Installation Process
- APP Startup Process
- Resource loading mechanism
- Gradle script
4, Android plug-in implementation of the current technical schools
- Dynamic replacement
- Static agent
- Dex Merge (Dex merge is the idea behind Android hot fixes)
5. Currently popular third-party plug-in framework
- Plug-in framework for 360: github.com/Qihoo360/Dr…
- Ali’s plug-in framework: github.com/alibaba/atl…
- Baidu Ren Yugang: github.com/singwhatiwa…
- Google’s son: Android App Bundles