Android performance optimization-code structure design pattern optimization

An interview question

BAT recruitment interview questions:
Can you write a singleton design pattern by hand? Analyze how it works.
What design patterns can you use proficiently? And analyze

The importance of design patterns

BAT recruitment requirements:

Baidu:
https://www.zhipin.com/job_detail/ef45ee4fcc18bca41Xx52NW1EFs~.html?ka=search_list_2
Ali:
https://www.zhipin.com/job_detail/c74b2b035fe446ae1Xd42t61Elo~.html?ka=search_list_7Huawei
:
https:// www.zhipin.com/job_detail/33bdb1207653b4571HR_0tW6ElM~.html?ka=search_list_15
Tencent:
https://www.zhipin.com/job_detail/e53ac0f672ca3b2c1HZ73t-7FFE~.html?ka=search_list_12

What is junk code

Characterization of junk code :
coupling of business code and technical code, coupling of
responsibilities of main business and value-added business code,
redundant dependencies

Problems :
Poor readability, poor
reusability, poor
maintainability, poor
changeability,
mutual dependence

For example: The
app directly references a third-party network request library. If this third-party network request library needs to be changed to another third-party network request library later, a lot of code needs to be modified.
Network request library: volley->okhttp->retrofit
image loading library: Android-Universal-Image-Loader -> picasso -> glide -> fresco

Architecture class diagram:
Insert picture description here

Object-oriented design principles

Design principles are the center, and design patterns are extensions of design principles.

Design principles (SOLID principles)

What are the design principles for garbage code cleanup?
The principle of opening and closing : open to expansion, and closed to modification. The function extension of the code is open, and the modification of the written code is closed;

Facade mode (appearance mode)

Definition: It is required that the communication between the outside of a subsystem and its inside must be carried out through a unified object. The facade mode provides a high-level interface, making the subsystem easier to use;

Purpose:
(1) The facade object is the only channel connecting the outside world and the inside of the subsystem;
(2) The program coupling
Insert picture description here
is reduced through the facade object; the factory is responsible for production and is not responsible for receiving customers. What should I do? Open a store and the store is responsible for receiving customers.
Insert picture description here

Facade pattern class diagram

Insert picture description here

Facade mode system architecture diagram

Decoupling: The client is decoupled from third-party libraries (subsystems), making it easier to expand and maintain the internal module functions of the subsystem.

Single responsibility: The client does not need to know what functions the third-party system provides or even how to use the third-party system. It only needs to interact with the intermediate facade class.

Architecture class diagram:
Insert picture description here

Advantages and disadvantages of decrypting facade mode

Insert picture description here

If you replace the third-party network request library and extend the program function, you need to modify the code of the original middle facade layer, which violates the opening and closing principle, what should I do? Introduce proxy mode!

Agency model

Definition: Provide a proxy object to the target object, and the proxy object controls the reference to the target object;
purpose:
(1) Indirect access to the target object by introducing the proxy object to prevent unnecessary direct access to the target object to the system Complexity;
(2) Enhance the original business through agency objects;

The products bought in the store cannot continue to meet the demand, and I want to buy advanced products with more functions, what should I do? Looking for purchasing!
Insert picture description here

Agent pattern class diagram

Insert picture description here

Restructuring

Architecture class diagram:
Insert picture description here

Use the proxy mode to refactor the code of the above example. Through the proxy mode, once the third-party network loading library is replaced, you only need to create a new middle facade layer and set it to the proxy object, so that the original middle The facade layer code can be discarded, which conforms to the principle of opening and closing.
Insert picture description here

to sum up

What is the use of design patterns?

To help us organize the application into an easy-to-understand, easy-to-maintain, and flexible architecture; the key to
building a maintainable system is to always think of the changes that the system may need in the future and the principles for coping with changes.

Insert picture description here

In addition, after understanding various design patterns, it will be easier to read the source code of many open source frameworks.

Summary of design patterns

Singleton mode
Decoration mode-FILE IO has a detailed analysis
Builder mode-Analysis in Okhttp source code analysis

Proxy mode-explained in the annotation Flyweight mode- handler source
code
Adapter mode-everywhere
Proxy mode-explained in the annotation/explained in the retrofit source code
Appearance mode-architecture isolation

Iterator mode-very widely used in containers
Observer design mode-skinned architecture has actual combat, rxjava
responsibility chain mode-Okhttp source code/event distribution source code

Insert picture description here
Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/yzpbright/article/details/109189849