2019最新《reactnative开发系列》

时间:2019-02-19
本文章向大家介绍2019最新《reactnative开发系列》,主要包括2019最新《reactnative开发系列》使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


接口:

public interface MyInterface {
    public boolean saySomething();
 
    public boolean doSomeThint();
}
接口实现:

public class MyImpl implements MyInterface {
 
    @Override
    public boolean saySomething() {
        System.out.println("MyImpl:saySomething()");
        return true;
    }
 
    @Override
    public boolean doSomeThint() {
        System.out.println("MyImpl:doSomething()");
        return true;
    }
 
}
静态代理实现:

public class StaticProxy {
    
    // 增加了日志功能的代理类
    private static class LogProxy implements MyInterface {
 
        private MyInterface myInterface;
 
        public LogProxy(MyInterface myInterface) {
            this.myInterface = myInterface;
        }
 
        public boolean saySomething() {
            System.out.println("BeforeLog:saySomething");
            boolean tmp = myInterface.saySomething();
            System.out.println("AfterLog:saySomething");
            return tmp;
        }
 
        public boolean doSomeThint() {
            System.out.println("BeforeLog:doSomething");
            boolean tmp = myInterface.doSomeThint();
            System.out.println("AfterLog:doSomething");
            return tmp;
        }
 
    }
 
    public static void main(String[] args) {
        MyInterface inf = new LogProxy(new MyImpl());
        inf.saySomething();
        inf.doSomeThint();
    }
 
}
LogProxy是代理类,目的是在访问接口中的方法时,在方法的前后加上日志,无需修改原始实现。使用静态内部类是为了控制类的可见性。

上边的实现方法与装饰器模式的实现一模一样,只是说法不一样而已。

2、动态代理
以上实现方式中,LogProxy只能代理MyInterface接口,它们的这种关系是编译阶段就确定的,所以称为静态代理。如果MyInterface接口修改了,那么相应的LogProxy代理也要跟着修改。

LogProxy的逻辑是在方法的前后加上日志,它应该是独立的逻辑,与具体的接口没有关系。Java中的动态代理机制就能实现这种解耦,LogProxy到底代理谁是在运行阶段确定的,因此是动态的,代码如下:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
 
public class DaynamicProxy {
 
    // 增加了日志功能的代理类
    static class LogProxy implements InvocationHandler {
 
        private Object target;
 
        public LogProxy(Object target) {
            this.target = target;
        }
 
        @Override
        public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
            System.out.println(proxy.getClass().getName());
            System.out.println("BeforeLog:" + method.getName());
            Object tmp = method.invoke(target, args);
            System.out.println("AfterLog:" + method.getName());
            return tmp;
        }
    }
 
    public static void main(String[] args) {
        // 到底代理谁在运行阶段指定
        InvocationHandler h = new LogProxy(new MyImpl());
        MyInterface inf = (MyInterface) Proxy.newProxyInstance(LogProxy.class.getClassLoader(),
                MyImpl.class.getInterfaces(), h);
        inf.saySomething();
        inf.doSomeThint();
    }
}
注意LogProxy实现了InvocationHandler接口,只需实现invoke一个方法就可以。
注意Proxy.newProxyInstance方法的调用,得到的MyInterface接口是被代理过的,效果与前边的实现方案相同。

不同点就是,现在LogProxy是独立的逻辑,不与具体的接口、具体的类相关,也就是它可以代理任何符合条件的类。


--------------------- 
作者:五星上炕 
来源:CSDN 
原文:https://blog.csdn.net/dkfajsldfsdfsd/article/details/86499009 
版权声明:本文为博主原创文章,转载请附上博文链接!