Android 经验谈:从retrofit来学动态代理

个人感觉,retrofit中的动态代理比较典型,我就拿出来解读一下:

先来阅读一下retrofit 的源码,看retrofit怎么来实现动态代理

ApiService apiService = retrofit.create(ApiService.class);

public <T> T create(final Class<T> service) {
    Utils.validateServiceInterface(service);
    if (validateEagerly) {
      eagerlyValidateMethods(service);
    }
    return (T) Proxy.newProxyInstance(service.getClassLoader(), new Class<?>[] { service },
        new InvocationHandler() {
          private final Platform platform = Platform.get();

          @Override public Object invoke(Object proxy, Method method, @Nullable Object[] args)
              throws Throwable {
            // If the method is a method from Object then defer to normal invocation.
            if (method.getDeclaringClass() == Object.class) {
              return method.invoke(this, args);
            }
            if (platform.isDefaultMethod(method)) {
              return platform.invokeDefaultMethod(method, service, proxy, args);
            }
            ServiceMethod<Object, Object> serviceMethod =
                (ServiceMethod<Object, Object>) loadServiceMethod(method);
            OkHttpCall<Object> okHttpCall = new OkHttpCall<>(serviceMethod, args);
            return serviceMethod.callAdapter.adapt(okHttpCall);
          }
        });
  }

retrofit这段代码主要作用是将类里的注解等参数解析,并包装成网络请求真正的数据,来进行请求数据。

咱模仿retrofit写一套动态代理:

定义注解:

@LeftFace
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited 
public @interface LeftFace {
    String value() default "左面脸";
}

 

@UpFace

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME) 
@Inherited  
public @interface UpFace {
    String value() default "上面脸";
}
创建接口
public interface IFaceListener {

    @LeftFace
    String getFace(String name);

    @UpFace
    String getFacePoint(String name);
}

创建动态代理

public class FaceCreate {

    public <T> T create(final Class<T> face){
        return (T) Proxy.newProxyInstance(face.getClassLoader(), new Class<?>[]{face}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                String result = null;
                if(method.isAnnotationPresent(LeftFace.class)){
                    LeftFace leftFace =  method.getAnnotation(LeftFace.class);
                    result = leftFace.value();
                }

                if(method.isAnnotationPresent(UpFace.class)){
                    UpFace upFace =  method.getAnnotation(UpFace.class);
                    result = upFace.value();
                }

                result = HString.concatObject(null,args)+result;
                return result;
            }
        });
    }

}

如此我们就模仿的建造了动态代理,动态代理在开发中相对与静态代理,灵活性更强。

解析 

new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) 


Object proxy:我们的真实对象
Method method:对象的方法 
Object[] args:对象的参数

Proxy.newProxyInstance(face.getClassLoader(), new Class<?>[]{face}, new InvocationHandler() {


ClassLoader loader:定义了由哪个ClassLoader对象来对生成的代理对象进行加载

Class<?>[] interfaces:一个Interface对象的数组,表示的是我将要给我需要代理的对象提供一组什么接口,如果我提供了一组接口给它,那么这个代理对象就宣称实现了该接口(多态),这样我就能调用这组接口中的方法了

InvocationHandler :InvocationHandler对象

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

二蛋和他的大花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值