最近接触了MVP,觉得比较有意思,之前学习了mvc,感觉MVP对于解耦方面不比mvc差,更重要的一点,感觉MVP比mvc在处理逻辑啥上面更清晰,把逻辑、界面、数据分离的更彻底,值得学习与研究一下。
下面是从一个开源项目学习与总结过来的基础知识:
MVpView:View个基础类。
public interface MvpView { }
public interface Mvppresenter<V extends MvpView> { public void attachView(V view); public void detachView(); }
public class MvpBasepresenter<V extends MvpView> implements Mvppresenter<V>{ private WeakReference<V> reference; @Override public void attachView(V view) { if(view==null) throw new NullPointerException("view can not be null when in attachview() in MvpBasepresenter"); else { if(reference==null) reference=new WeakReference<V>(view); } } @Override public void detachView() { if(reference!=null) reference.clear(); } public V getMvpView() { if(reference!=null&&reference.get()!=null) return reference.get(); else throw new NullPointerException("have you ever called attachview() in MvpBasepresenter"); } public Boolean isAttach() { return reference!=null&&reference.get()!=null; } }
public interface BasemvpDelegateCallback <P extends Mvppresenter,V extends MvpView>{ public void setPresenter(); public P getPresenter(); public P createPresenter(); public V getMvpView(); }
public class MvpInternalDelegate<P extends Mvppresenter, V extends MvpView> { BasemvpDelegateCallback<P, V> callback; public MvpInternalDelegate(BasemvpDelegateCallback<P, V> callback) { this.callback = callback; } public P creatPresneter() { P p=callback.getPresenter(); if (p== null) p=callback.createPresenter(); if(p==null) throw new NullPointerException("callback.createPresenter() is null in MvpInternalDelegate"); return p; } public void attachView() { callback.getPresenter().attachView(callback.getMvpView()); } public void detacthView() { callback.getPresenter().detachView(); } }
接下来是如何使用,以activity为例:
1.创建一个ActyvityDelegate,与activity生命周期一致,目的是为了通过activity的生命周期去控制是否要attachView:
public interface ActyvityDelegate { public void onCreate(); public void onPause(); public void onResume(); public void onStop(); public void onDestroy(); }
public interface ActivityMvpDelegateCallback<P extends Mvppresenter,V extends MvpView> extends BasemvpDelegateCallback<P,V> { }
public class ActivityDelegateImp<P extends Mvppresenter,V extends MvpView> implements ActyvityDelegate { private BasemvpDelegateCallback<P,V> basemvpDelegateCallback; private MvpInternalDelegate<P,V> mvpInternalDelegate; public ActivityDelegateImp(BasemvpDelegateCallback<P,V> basemvpDelegateCallback) {//传入BasemvpDelegateCallback 去控制presenter if(basemvpDelegateCallback==null) throw new NullPointerException("the basemvpDelegateCallback in ActivityDelegateImpn is null"); this.basemvpDelegateCallback=basemvpDelegateCallback; mvpInternalDelegate=new MvpInternalDelegate<P,V>(this.basemvpDelegateCallback); } @Override public void onCreate() { mvpInternalDelegate.creatPresneter(); mvpInternalDelegate.attachView(); } @Override public void onPause() { } @Override public void onResume() { } @Override public void onStop() { } @Override public void onDestroy() { mvpInternalDelegate.detacthView(); } }
public abstract class MvpActivity<P extends Mvppresenter, V extends MvpView> extends Activity implements ActivityMvpDelegateCallback<P, V>, MvpView { private ActyvityDelegate mActyvityDelegate; private P mPresenter; public MvpActivity() { super(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mActyvityDelegate = new ActivityDelegateImp<P, V>(this); mActyvityDelegate.onCreate(); } @Override protected void onResume() { super.onResume(); mActyvityDelegate.onResume(); } @Override protected void onPause() { super.onPause(); mActyvityDelegate.onPause(); } @Override protected void onStop() { super.onStop(); mActyvityDelegate.onStop(); } @Override protected void onDestroy() { super.onDestroy(); mActyvityDelegate.onDestroy(); } @Override public void setPresenter() { } protected abstract P CreatePresenter();//暴露一个创建的方法用于创建presenter @Override public P createPresenter()//这个方法由MvpInternalDelegate 调用<span style="font-family: Arial, Helvetica, sans-serif;">BasemvpDelegateCallback 来创建presenter</span> { mPresenter=CreatePresenter(); return mPresenter; } @Override public P getPresenter() {// return mPresenter; } @Override public V getMvpView() { return (V) this; } }
public class MyMvpActivity extends MvpActivity<MyActivityPresenter, ActivityMvpView> implements ActivityMvpView { protected RelativeLayout mLoadinglayout; protected RelativeLayout mMsglayout; protected TextView mMsgtextview; public MyMvpActivity() { super(); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mymvp_activity_layout); findview(); } private void findview() { mLoadinglayout = (RelativeLayout) findViewById(R.id.my_mvpactovity_loadinglayout); mMsglayout = (RelativeLayout) findViewById(R.id.my_mvpactovity_errormsglayout); mMsgtextview = (TextView) findViewById(R.id.my_mvpactovity_errormsg_textview); } @Override public void showLoading() { mLoadinglayout.setVisibility(View.VISIBLE); mMsglayout.setVisibility(View.GONE); } @Override public void showContent() { } @Override public void showerror() { mLoadinglayout.setVisibility(View.GONE); mMsglayout.setVisibility(View.VISIBLE); } @Override public void setdata() { } @Override protected MyActivityPresenter CreatePresenter() { return new MyActivityPresenter(); } }
明显,ActivityMvpView是MvpView的子类:
public interface ActivityMvpView extends MvpView{ public void showLoading(); public void showContent(); public void showerror(); public void setdata(); }
public class MyActivityPresenter extends MvpBasepresenter<ActivityMvpView> { public MyActivityPresenter() { super(); } public void setdata() { getMvpView().showerror(); } public void showdata() { getMvpView().showLoading(); } }