码迷,mamicode.com
首页 > 其他好文 > 详细

BasePresenter 基类

时间:2019-08-04 19:33:11      阅读:97      评论:0      收藏:0      [点我收藏+]

标签:com   alt   warning   trace   对象   pac   alc   void   ase   

package com.test.mvp.mvpdemo.mvp.v4.basemvp;

import android.util.Log;

import java.lang.ref.SoftReference;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Proxy;
import java.lang.reflect.Type;

public abstract class BasePresenter<V extends IBaseView, M extends BaseModel> implements IBasePresenter {
private SoftReference<IBaseView> mReferenceView;
private V mProxyView;
private M mModel;

@SuppressWarnings({"unchecked", "TryWithIdenticalCatches"})
@Override
public void attach(IBaseView view) {
//使用软引用创建对象
mReferenceView = new SoftReference<>(view);
//使用动态代理做统一的逻辑判断 aop 思想
mProxyView = (V) Proxy.newProxyInstance(view.getClass().getClassLoader(), view.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
if (mReferenceView == null || mReferenceView.get() == null) {
return null;
}
return method.invoke(mReferenceView.get(), objects);
}
});

//通过获得泛型类的父类,拿到泛型的接口类实例,通过反射来实例化 model
ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
if (type != null) {
Type[] types = type.getActualTypeArguments();
try {
mModel = (M) ((Class<?>) types[1]).newInstance();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
e.printStackTrace();
}
}

}

@SuppressWarnings("unchecked")
public V getView() {
return mProxyView;
}

protected M getModel() {
return mModel;
}

@Override
public void detach() {
mReferenceView.clear();
mReferenceView = null;
}
}
--------------------- 

BasePresenter 基类

标签:com   alt   warning   trace   对象   pac   alc   void   ase   

原文地址:https://www.cnblogs.com/hyhy904/p/11299089.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!