Android MVP模式实现基类优化

1.添加依赖,我这里使用rxjava+retrofit2作为例子,模仿网络请求

    compile 'io.reactivex:rxandroid:1.0.1'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.2'

2.创建基类BaseMvpView.java

public interface BaseMvpView {
    void showToast(String s);
}

3.创建基类BaseMvpModel.java,添加和移除Subscriotion,防止内存泄漏

public class BaseMvpModel {
    private static final String TAG = "BaseMvpModel";
    protected final CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    public void addSubscriber(Subscription subscription) {
        mCompositeSubscription.add(subscription);
        Log.e(TAG, "addSubscriber: 添加");
    }

    public void removeSubscriber() {
        Log.e(TAG, "removeSubscriber: 移除");
        if (!mCompositeSubscription.isUnsubscribed()) {
            mCompositeSubscription.unsubscribe();
        }
    }
}

4.创建基类BaseMvpresenter.java,将BaseMvpView和BaseMvpModel关联起来

public abstract class BaseMvpPresenter {

    private V view;
    public final M model;

    public BaseMvpPresenter() {
        this.model = createModel();
    }
    //关联view层,得到view层的对象
    public abstract void attach(V v);
    //取消关联,移除网络请求操作,避免内存泄漏
    public abstract void detach();
    //创建对应的M层
    public abstract M createModel();

    public V getView() {
        return view;
    }

    public void setView(V view) {
        this.view = view;
    }
}

5.创建基类BaseMvpActivity.java,所有要使用MVP模式的Activity继承这个父类

public abstract class BaseMvpActivity extends AppCompatActivity {

    public V presenter;
    public K mvpView;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        initMvp();
        super.onCreate(savedInstanceState);
    }

    private void initMvp() {
        presenter = createPresenter();
        mvpView = createMvpView();
        if (presenter != null && mvpView != null) {
            presenter.attach(mvpView);//关联
        }
    }
    //让子类实现,创建对应的P对象
    public abstract V createPresenter();
    //让子类实现,创建对应的V对象
    public abstract K createMvpView();

    @Override
    protected void onDestroy() {
        if (presenter != null) {//取消关联
            presenter.detach();
        }
        super.onDestroy();
    }
}

6.使用

1)创建LoginView继承BaseMvpView

public interface LoginView extends BaseMvpView {
    void getLoginSuccessful(MvpBean mvpBean);//参数自己定义
    void getLoginFailed(String s);
}

2)创建LoginModel继承BaseMvpModel

public class LoginModel extends BaseMvpModel{
    private static final String TAG = "LoginModel";

    public void dealHttpRequest(Subscriber subscriber) {//模拟网络请求
        Subscription subscribe = Observable.timer(3, TimeUnit.SECONDS).flatMap(new Func1>() {
            @Override
            public Observable call(Long aLong) {
                Observable mvpBeanObservable = Observable.create(new Observable.OnSubscribe() {
                    @Override
                    public void call(Subscriber subscriber) {
                        MvpBean mvpBean = new MvpBean();
                        mvpBean.setCode(100);
                        subscriber.onNext(mvpBean);
                        subscriber.onCompleted();
                    }
                });
                return mvpBeanObservable;
            }
        }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(subscriber);
        //添加网络请求
        addSubscriber(subscribe);
    }
}

3)创建LoginPresenter继承BaseMvpPresenter

public class LoginPresenter extends BaseMvpPresenter {

    public void TestGetApiData() {//请求数据
        model.dealHttpRequest(new Subscriber() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                getView().getLoginFailed(e.getMessage());
                getView().showToast(e.getMessage());
            }

            @Override
            public void onNext(MvpBean mvpBean) {
                getView().getLoginSuccessful(mvpBean);
            }
        });
    }


    @Override
    public void attach(LoginView testView) {
        setView(testView);//设置View层对象
    }

    @Override
    public void detach() {
        //移除网络请求
        model.removeSubscriber();
    }

    @Override
    public LoginModel createModel() {
        return new LoginModel();//创建M层对象
    }
}

4)创建TestActivity继承BaseMvpActivity

public class TestActivity extends BaseMvpActivity {

    private static final String TAG = "TestActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);
        presenter.TestGetApiData();
    }

    @Override
    public LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    public LoginView createMvpView() {
        return mTestView;
    }

    private LoginView mTestView = new LoginView() {
        @Override
        public void showToast(String s) {

        }

        @Override
        public void getLoginSuccessful(MvpBean mvpBean) {
            Log.e(TAG, "success: " + mvpBean.getCode());
        }

        @Override
        public void getLoginFailed(String s) {

        }
    };

}


你可能感兴趣的:(android)