美文网首页MVP
【项目FJU】MVP之BaseMVP基础框架实例

【项目FJU】MVP之BaseMVP基础框架实例

作者: 秀叶寒冬 | 来源:发表于2019-09-14 11:19 被阅读0次

    MVP目录结构

    • 基础模块


      基础模块

    项目目前主要分为4个模块,fjslibrary主要是公共类库,fjstest是用于自动化测试代码,mainmodule为主模块,app为项目壳工程。

    • fjslibrary模块


      图片.png

    主要是MVP的基础模块

    • mainmodule


      图片.png

    实际使用的MVP示例。

    fjslibrary模块BaseMVP源码

    • IBaseView
    package com.yds.jianshu.base.mvp;
    
    import android.content.Context;
    
    /**
     * Created by yds
     * on 2019/8/3.
     */
    public interface IBaseView {
        Context getContext();
    }
    
    
    • IBasePresenter
    package com.yds.jianshu.base.mvp;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public interface IBasePresenter<V extends IBaseView> {
        /**
         * used to attach the real View
         * @param view
         */
        void attach(V view);
    
        /**
         * used to detach the real View
         */
        void detach();
    }
    
    

    这里使用泛型,V代码继承自IBaseView的View类型,attach主要用于与View绑定,detach用于与View解绑,传统View是在presenter初始化时初始一个对应的View,但这样view就会和Presenter耦合,使用泛型能使得view和presenter耦合度降低,且利于重用。

    • BaseModel
    package com.yds.jianshu.base.mvp;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public abstract class BaseModel {
    
    }
    
    
    • BasePresenter
    package com.yds.jianshu.base.mvp;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public abstract class BasePresenter<V extends IBaseView> implements IBasePresenter{
        protected V mView;
        @SuppressWarnings("unchecked")
        @Override
        public void attach(IBaseView view) {
            mView = (V) view;
        }
    
        @Override
        public void detach() {
            mView = null;
        }
    }
    
    

    写一个BasePresenter抽象类,该类实现了IBasePresenter接口,声明一个View,并在attach时绑定,在detach时释放。

    • BaseActivity
    package com.yds.jianshu.base.mvp;
    
    import android.app.Activity;
    import android.content.Context;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    
    /**
     * Created by yds
     * on 2019/8/3.
     */
    public abstract class BaseActivity<P extends IBasePresenter> extends Activity implements IBaseView{
        protected P mPresenter;
        protected abstract void initLayout(@Nullable Bundle savedInstanceState);
        protected abstract void initViews();
        protected abstract void initEvents();
    
        protected abstract P setPresenter();
    
        @Override
        protected void onCreate(@Nullable Bundle saveInstanceState){
            super.onCreate(saveInstanceState);
            initLayout(saveInstanceState);
            mPresenter = setPresenter();
            if(mPresenter!=null){
                mPresenter.attach(this);
            }
            initViews();
            initEvents();
    
        }
        @Override
        protected void onDestroy() {
            super.onDestroy();
            mPresenter.detach();
            mPresenter = null;
        }
    
        @Override
        public Context getContext() {
            return this;
        }
    }
    
    

    MVP的View指的是Activity及Fragment等,创建一个BaseActivity,并在BaseActivity中初始化一个Presenter,并与BaseActivity绑定

    mainmodule中使用

    package com.yds.mainmodule.mobile.contract;
    
    import com.yds.jianshu.base.mvp.IBasePresenter;
    import com.yds.jianshu.base.mvp.IBaseView;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public interface MainContract {
        interface IMainModel{
    
        }
        interface IMainView extends IBaseView{
    
        }
        interface IMainPresenter extends IBasePresenter{
    
        }
    }
    
    

    创建一个MainContract接口,该接口中声明了MainMVP中所需要的model、view以及presenter,一目了然。

    • MainModel
    package com.yds.mainmodule.mobile.models;
    
    import com.yds.jianshu.base.mvp.BaseModel;
    import com.yds.mainmodule.mobile.contract.MainContract;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public class MainModel extends BaseModel implements MainContract.IMainModel {
    }
    
    

    可以在MainModel中做一些网络请求,数据处理等操作。

    • MainPresenter
    package com.yds.mainmodule.mobile.presenters;
    
    import com.yds.jianshu.base.mvp.BasePresenter;
    import com.yds.jianshu.base.mvp.IBaseView;
    import com.yds.mainmodule.mobile.contract.MainContract;
    
    /**
     * Created by yds
     * on 2019/8/29.
     */
    public class MainPresenter extends BasePresenter<MainContract.IMainView> implements MainContract.IMainPresenter {
        private MainContract.IMainModel mModel;
        @Override
        public void attach(IBaseView view) {
            super.attach(view);
        }
    
        @Override
        public void detach() {
            super.detach();
        }
    }
    
    

    可以在MainPresenter 中做一些数据逻辑等操作,处理model传递来的数据并将数据返回给view层。

    • MainActivity
    package com.yds.mainmodule.mobile.views;
    
    import android.content.Intent;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    
    import com.yds.jianshu.base.mvp.BaseActivity;
    import com.yds.mainmodule.R;
    import com.yds.mainmodule.mobile.contract.MainContract;
    import com.yds.mainmodule.mobile.presenters.MainPresenter;
    import com.yds.mainmodule.onepixel.OnePixelService;
    
    public class MainActivity extends BaseActivity<MainContract.IMainPresenter> implements MainContract.IMainView{
        private static final String TAG = "[MainActivity]";
    
        @Override
        protected void initLayout(@Nullable Bundle savedInstanceState) {
            setContentView(R.layout.activity_main);
        }
    
        @Override
        protected void initViews() {
    
        }
    
        @Override
        protected void initEvents() {
            startOnePixelService();
        }
    
        @Override
        protected MainContract.IMainPresenter setPresenter() {
            return new MainPresenter();
        }
    
        private void startOnePixelService(){
            Intent intent = new Intent();
            intent.setClass(MainActivity.this, OnePixelService.class);
            startService(intent);
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
        }
    }
    
    

    MainActivity即View层,在这里不用再对Presenter进行初始化即绑定,实现setPresenter方法即可,其初始化及绑定被封装在BaseActivity中。

    源码地址

    源码:https://github.com/Yedongsheng/Jianshu/tree/develop

    相关文章

      网友评论

        本文标题:【项目FJU】MVP之BaseMVP基础框架实例

        本文链接:https://www.haomeiwen.com/subject/vdnwectx.html