1. Android architecture design mode

  • MVC architecture design pattern: THE full name of MVC is Model View Controller, which is the abbreviation of Model – View – Controller.
  • MVP architecture design mode: MVC full name is Model View Persenter, MVP evolved from MVC, is now the mainstream development mode.
  • MVVM Architectural Design Pattern: THE MVVM full name is Model-view-ViewModel, which is essentially an improved version of MVC.

The main purpose of each Model is to separate the View from the Model, that is, the UI display from the business logic.

1.1 Architectural design pattern-MVC

(1) definition:

In the android development process, the popular development framework used to adopt the MVC framework pattern.

  • M (Model) layer: Solid Model, processingThe business logic. For example, database operations, network operations, I/O operations, complex operations, and time-consuming tasks.
  • V (View) layer: processingAccording to data. In Android development, it generally corresponds toXML layoutFile.
  • C (Controller) layer: processingThe user interaction. In Android development, it generally corresponds toActivity/Feagment. Android mainly processes user interaction and business logic through activity, accepts user input and calls Model and View to fulfill user requirements.

(2)

  • Low coupling
  • Reusable and easy to expand
  • Module responsibilities are clearly divided

(3) the instance

The design structure of Android itself conforms to the MVC pattern.

(4) Advantages and disadvantages of MVC

  • Advantages of MVC: The MVC pattern controls the overall situation through the Controller, while separating the View presentation from the Model changes
  • MVC also has limitations:

The View layer can do very little with XML layout files, so you need to move most of your view-related operations to the Controller layer activities. As a result, the activity plays two roles (the View layer and the Controller layer), handling not only the business logic but also the UI. As a page becomes more and more complex, the activity code becomes more and more bloated and complex.

1.2 Architecture Design Pattern -MVP

MVP evolved from the classic MVC pattern. Their basic ideas are similar :Controller/Presenter handles logic, Model provides data, and View displays. In Android development, the MVP implementation process is that when a Presenter receives a View request, it obtains data from the Model layer and processes the data. The processed data is then called back to the Activity or Fragment via the View layer interface. This MVP allows an Activity or Fragment to become a true View, doing only uI-related things and not other business processes.

(1) the definition

  • M (Model) layer: Solid Model, processingThe business logic. For example, database operations, network operations, I/O operations, complex operations, and time-consuming tasks.
  • V (View) layer: responsibleView drawing and user interaction. In Android development, it generally corresponds toXML layout file and Activity/Fragment.
  • P (Presenter) layer: Is responsible for data between the Model layer and View layerinteractionandThe business logic.

(2) instance

(3) Difference between MVC and MVP

MVP views don’t use the Model directly, they communicate with each other through the Presenter, and all interaction takes place inside the Presenter, whereas MVC views read data directly from the Model rather than through the Controller

  • The biggest difference between MVC and MVP: MVC’s Model layer and View layerBe able to interact directly; MVP’s Model layer and View layerCan't interact directlyThe Presenter layer is used for interaction.
  • Activities have different responsibilities: An Activity belongs to the Controller layer in MVC and the View layer in MVP, which is a major difference between MVC and MVP. Android’s shift from MVC to MVP development is also a major oneOptimize your Activity code to avoid Activity code bloatiness.
  • The View layer is different: MVC’s View layer refers to the XML layout file (or Java custom View); MVP’s View layer is an Activity (or Fragment)
  • The control layer is different: MVC control layer is Activity (or Fragment); MVP’s control layer is Presenter, which doesn’t have a lot of real stuff in it, and is responsible for the interaction between the Model layer and the View layer.

(4) MVP pros and cons

  • The MVP has the following advantages:

The model is completely separated from the view, and we can modify the view without affecting the model; Project code structure is clear, a look to know what classes do things; We can use a Presenter for multiple views without changing the Presenter logic, which is very useful because views change more often than models. Work collaboratively (for example, write some business logic code before the designer comes up with a diagram)

  • The MVP also has its downsides:

Too many interfaces affect coding efficiency to a certain extent. To some extent, Presenter has too much code. To reduce the complexity of presenters, Google has introduced MVVM, a data-driven approach to reduce the amount of code presenters need.

1.3 Architecture design pattern -MVVM

(1) the definition

  • M (Model) layer: StillEntity model(but different from the previously defined Model layer), is responsible for data fetching, storing, and changing, and provides a data interface for the ViewModel layer to invoke.
  • V (View) layer: correspondingActivity/FeagmentXML layoutFile, responsible for View drawing and user interaction note: View layer can onlyOperating the UI(Data binding for UI updates) You cannot do any data manipulation related to business logic
  • VM (ViewModel) layer: Responsible for completing data between the Model layer and the View layerinteractionandThe business logicNote: The ViewModel layer can only do andThe business logicRelevant data operations; Cannot do UI-related operations

2. Android plug-in

Reason for plug-in: With the increase of business, more business logic code, APK package also gradually increased, which is not conducive to maintenance and upgrade. Functional modules can be decoupled through plug-in development, so that different maintenance teams can only maintain services of a certain module. At the same time, when THE APP is upgraded, only a certain functional module can be upgraded instead of the overall upgrade.

2.1 Plug-ins to solve the problem – how to load APK dynamically

(1) Android class loader and differences

What the class loader does: Java bytecode is loaded into the Java virtual machine through the class loader.

  • PathClassLoader: only apK in the file directory can be loaded.
  • DexClassLoader: can load bytecodes in APK files (load Java bytecodes from dex entity JAR files). Mainly used for dynamic loading and code hot update and so on.

(2) Reflection: Reflection in Java allows us to obtain the properties, methods, and internal information mechanism of this class at runtime. Most importantly, we can instantiate this object to call methods at runtime, which is the biggest advantage of Java reflection. (3) Realize dynamic loading apK

What is dynamic loading of APK? There is a speed program in Android that automatically loads apK on a specified SD card and executes it through a proxy activity.

Implementation: Need a proxy activity to execute the activity in APK, mainly through reflection to obtain its properties and methods, so as to make apK calls. Implementation principle: Classloader (loading classes) + reflection (getting properties and methods) + dynamic proxy (execution)

Resource sharing

Click:

** Android Architecture Video +BAT Interview Topics PDF+ Study Notes ** available for free

There is a lot of information about Learning Android online, but if the knowledge learned is not systematic, and problems are only tasted, no further research, then it is difficult to achieve real technology improvement. I hope this systematic technical system has a direction reference for everyone.

2020 May be a bumpy year for Android, but don’t panic. Make your own plans and learn your own lessons. Competition is everywhere, in every industry. Believe in yourself, there is nothing impossible, only unexpected. I wish you all the best in 2021.