zoukankan      html  css  js  c++  java
  • BasePresenter 基类

    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;
    }
    }
    --------------------- 

  • 相关阅读:
    安装IIS
    安装Asp.Net(4.0.30319)
    转载一个博文
    文件操作引出流(二)FileStream和
    《学习之道》第十一章目前此章最后一点-重复
    《学习之道》第十一章意群
    Views
    Django的基本使用
    MVC框架
    Zookeeper
  • 原文地址:https://www.cnblogs.com/hyhy904/p/11299089.html
Copyright © 2011-2022 走看看