Android AOP Overview

What AOP is?

Many people online are introducing AOP says: Aspect Oriented Programming, by way of pre-compiler and run-time dynamic agent technology to implement a unified program functions maintenance. Personally I think that this sentence is wrong. AOP and OOP as a programming idea, rather than technological means.

There are six principles of programming, which is the first principle of single responsibility principle. It means that a class is only responsible for one thing. This is in keeping with the characteristics of the package of OOP. Under this condition, our program will be dispersed to different classes, different ways to go. The benefit of this is to reduce the complexity of the class, improve the maintainability of the program. But at the same time, it also makes the code becomes long-winded. For example, we want to add as a method call log, it must add log calls to all methods of all classes, even though they are the same. In order to solve the above problem, AOP came into being.

AOP aims to cross-cutting concerns with the main business classification, thereby increasing the modularity of program code. Crosscutting concerns is an abstract concept, it refers to those services through multiple modules in the project. The last example logging is a typical cross-cutting concerns.

Several AOP implementations

Dynamic Proxy

Dynamic Proxy is a design pattern. It has the following features:

  • We do not need to write your own proxy class.

  • Runtime generates a proxy object directly through the interface.

  • Before deciding which object agent during operation.

For example in the following example, we look at the structure of dynamic proxy class diagram.

To access later Usually we have a part of the function of APP require users to log. Such as changing passwords, modify the user name and other functions. When a user intends to use these features, we are generally on the user's login state judge, only the user logged in, in order to properly use these features. If the user is not logged in, our APP to jump to the login page. Take, for example to change the password we look at the dynamic proxy class diagram.

image

InvocationHandler is to provide a dynamic Java JDK agent inlet, the method used for the processing of the proxy object.
code show as below:


    public static class LoginCheckHandler implements InvocationHandler {

        private static <S, T extends S> T proxy(S source, Class<T> tClass) {
            return (T) Proxy.newProxyInstance(Main.class.getClassLoader(), new Class[]{tClass}, new LoginCheckHandler(source));
        }

        private Object mSource;

        LoginCheckHandler(Object source) {
            this.mSource = source;
        }

        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            if(!checkLogin()){
                jumpToLoginActivity();
                return null;
            }
            return method.invoke(mSource, args);
        }

        private boolean checkLogin(){
            System.out.println("用户未登录");
            return false;
        }

        private void jumpToLoginActivity(){
            System.out.println("跳转到登录页");
        }
    }

    public class Client {

        public static void main(String[] args) {
            IUserSetting source = new UserSetting();
            IUserSetting iUserSetting = LoginCheckHandler.proxy(source,IUserSetting.class);
            iUserSetting.changePwd("new Password");
        }
    }

After this package, check the login page Login Jump logic as crosscutting concerns on the subject of business and were separated. When new requirements need to log in to check the time, we only need to generate a new proxy object through LoginCheckHandler.

APT

APT (Annotation Processing Tool) is a compile-time annotation processing technology. It is achieved by defining annotation processor and compiler of the code generation, and the generated code and the source code is compiled into .class files together. By APT technique, we crosscutting concerns into the annotation processor package in order to achieve separation of crosscutting concerns business body. A more detailed description please move on Android compile instrumentation that allows an application to write code for (a).

AspectJ

AspectJ is a kind of a compiler, it increases the keyword identification and compilation method based on the Java compiler. Therefore, AspectJ compile Java code. It also provides Aspect program. During compilation, the program developers to write Aspect woven into the target program, the extension of the target program. Developers to implement AOP functionality by writing AspectJ programs. A more detailed description please move on Android compile instrumentation, let the program write their own codes (two).

Transform + Javassist/ASM

Transform Android Gradle is provided, a way to operate bytecode. App compilation, the source code is compiled into class first, and then be compiled into dex. In the class compiled into dex process, we will go through a series of Transform processing. Javassist / ASM is a library can very easily operate bytecode. We can modify the compiled .class file through them. In this way, we will encapsulate crosscutting concerns Transform, to achieve the purpose of separation of the main business. A more detailed description please move on Android compile instrumentation that allows an application to write the code (C).

We can use to achieve what functions?

Use AOP can do some very interesting things. Some well-known open source framework, they have adopted the idea of ​​AOP. For example: ButterKnife, Retrofit, Hugo like. In addition, AOP appeared contending situation Buried in performance testing and technology.

  • Performance testing and optimization. ArgusAPM, drops the booster, Hugo Jake Wharton Great God 360.

  • Buried technology. DDAutoTracker Luo Ji thinking, Sensors Analytics data plan of God, Netease HubbleData and so on.

In addition, with the AOP we can achieve the following functions:

  • Usually when we request data to the server, it will display a Loding, wait until results are returned after hiding it. We can AOP technology to show, hide Loding action and separated from the main business.

  • authority management. We recommend an open source library Aopermission, with AspectJ solve the permissions problem.

For more information + interview Android development framework to share information click on the link to receive free

"Android Architect necessary to receive a free learning resources (architecture + video + interview thematic document study notes)"

Guess you like

Origin blog.csdn.net/Coo123_/article/details/93126593