美文网首页
MVP设计模式和基本的模板

MVP设计模式和基本的模板

作者: AndyWei123 | 来源:发表于2017-07-27 22:53 被阅读0次

    MVP设计模式和基本的模板

    - View:Activity、Fragment、Dialog、Adapter等,该层不包含任何业务逻辑
    - 
    - Presenter:中介,View 与 Model 不发生联系,都通过 Presenter 传递
    - 
    - Model:相关的业务操作处理数据(如对数据库的操作、对网络等的操作都应该在Model层里)
    -
    
    Paste_Image.png

    这个是我的项目结构

    Paste_Image.png

    对比于MVC模式,MVP无疑更适合Android的开发,MVP在层次结构上更适合清晰View只能和Presenter进行交换而且是双向的。Modle也只能和Presnter进行交互,无疑降低了代码的耦合性。
    下面是在MVP模式下基本的模板

    Iview

    public interface IView<P> {
         P createPresenter();
    }
    

    Iview的方法只有一个就是创建一个Present

    BaseMode

    public abstract class BaseModel<P extends BasePresenter> {
        public P mPresenter;
        public BaseModel(P presenter) {
            this.mPresenter = presenter;
        }
    }
    

    BaseMode里面有自己的Present,而且在创建Mode的时候必须创建一个Present,由于使用泛型就不用再自己创建一遍,由于java的单继承其实我将它写成抽象类也很无奈。

    BasePresenter

    public abstract class BasePresenter<V extends IView, M extends BaseModel> {
        public V mView;
        public M mModel;
        public void attach(V view) {
            mView = view;
            mModel = createModel();
        }
        public abstract M createModel();
        public void detach() {
            mView = null;
        }
    }
    

    在Presenter里面持有自己的view和Modle 并且有一个attach方法 在view创建Present后就进行attach
    而在view销毁时进行detach,而attch时候创建了自己的Modle并持有了view

    下面是最重要的Activity和Fragment

    BaseActivity

    public abstract class BaseActivity extends AppCompatActivity {
        protected final String TAG = getClass().getSimpleName();
        public static BaseActivity activity;
        private Unbinder mUnbinder;
        public FragmentMgr fragmentMgr;
        protected SystemBarTintManager sm;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            activity = this;
            ((MyApplication) MyApplication.getContext()).addActivity(this);
            setContentView(getLayoutRes());
            init();
            sm = new SystemBarTintManager(this);
            sm.setStatusBarTintEnabled(true);
            sm.setStatusBarTintColor(getResources().getColor(R.color.colorPrimaryDark));
        }
    
        @Override
        public void setContentView(@LayoutRes int layoutResID) {
            super.setContentView(layoutResID);
            mUnbinder = ButterKnife.bind(this);
        }
    
        @Override
        public void setContentView(View view) {
            super.setContentView(view);
            mUnbinder = ButterKnife.bind(this);
        }
    
        protected abstract int getLayoutRes();
    
        @Override
        protected void onResume() {
            super.onResume();
            activity = this;
        }
    
        @Override
        protected void onPause() {
            super.onPause();
            activity = null;
    
        }
    
        private void init() {
            initData();
            initEvents();
        }
    
        /***
         * 初始化事件(监听事件等事件绑定)
         */
        protected void initEvents() {
        }
    
        /**
         * 绑定数据
         */
        protected void initData() {
        }
    
        /**
         * activity退出时将activity移出栈
         */
        @Override
        protected void onDestroy() {
            mUnbinder.unbind();
            super.onDestroy();
            ((MyApplication) MyApplication.getContext()).removeActivity(this);
        }
    
    }
    

    BaseAcitvity想必大家都有自己的模板,我这里的BaseActivity主要是将setcontentlayout 和界面的初始化数据还有相应时间分割开来

    MVPActivity

    public abstract class MvpFragment<P extends BasePresenter> extends BaseFragment implements IView<P> {
    
        public P mPresenter;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPresenter = createPresenter();
            if (mPresenter != null) {
                mPresenter.attach(MvpFragment.this);
            }
        }
    
        @Override
        public void onDestroy() {
            if (mPresenter != null) {
                mPresenter.detach();
            }
            super.onDestroy();
        }
    }
    

    这里的主要是创建一个Presenter并且attach他还有在destory的时候detch

    BaseFragment

    public abstract class BaseFragment extends Fragment {
    
        Unbinder mUnbinder;
    
        Toolbar mToolbar;
    
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
            Log.d("BaseFragment",this.getClass().getSimpleName()+"onCreateView");
            return super.onCreateView(inflater, container, savedInstanceState);
        }
    
    
        @Override
        public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            mUnbinder = ButterKnife.bind(this, view);
            mToolbar = (Toolbar) view.findViewById(R.id.toolbar);
            init();
            Log.d("BaseFragment", this.getClass().getSimpleName()+"onViewCreated");
        }
    
        private void init() {
            initEvent();
            initData();
        }
    
        protected abstract void initEvent();
    
        protected abstract void initData();
    
        protected void initToolbar(String title) {
            if (mToolbar != null) {
                mToolbar.setTitle(title);
                ((AppCompatActivity) getActivity()).setSupportActionBar(mToolbar);
            }
            ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
            if (actionBar != null) {
                actionBar.setDisplayHomeAsUpEnabled(true);
            }
        }
    
        @Override
        public void setMenuVisibility(boolean menuVisible) {
            super.setMenuVisibility(menuVisible);
            if (this.getView() != null)
                this.getView().setVisibility(menuVisible ? View.VISIBLE : View.GONE);
        }
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.d("BaseFragment", this.getClass().getSimpleName()+ "onCreate");
        }
    
        @Override
        public void onPause() {
            super.onPause();
            Log.d("BaseFragment", this.getClass().getSimpleName()+ "onPause");
        }
    
        @Override
        public void onStart() {
            super.onStart();
            Log.d("BaseFragment", this.getClass().getSimpleName()+ "onStart");
        }
    
        @Override
        public void onDestroy() {
            mUnbinder.unbind();
            super.onDestroy();
            Log.d("BaseFragment",this.getClass().getSimpleName()+"onDestroy");
        }
    
        @Override
        public void onAttach(Context context) {
            super.onAttach(context);
            Log.d("BaseFragment", this.getClass().getName() + "onAttach");
        }
    
        @Override
        public void onDestroyView() {
            super.onDestroyView();
            Log.d("BaseFragment", this.getClass().getSimpleName()+ "onDestroyView");
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            Log.d("BaseFragment", this.getClass().getSimpleName()+"onDetach");
        }
    }
    

    这里也是类似Activity 将初始化界面数据和点击事件分开。不使用butterknife的可以去掉

    MvpFragement

    public abstract class MvpFragment<P extends BasePresenter> extends BaseFragment implements IView<P> {
    
        public P mPresenter;
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            mPresenter = createPresenter();
            if (mPresenter != null) {
                mPresenter.attach(MvpFragment.this);
            }
        }
    
        @Override
        public void onDestroy() {
            if (mPresenter != null) {
                mPresenter.detach();
            }
            super.onDestroy();
        }
    }
    

    这里类似于MVPActivity

    相关文章

      网友评论

          本文标题:MVP设计模式和基本的模板

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