您的位置:首页 > 移动开发 > Android开发

MVP的简单实例

2017-01-09 10:55 288 查看
Android MVP简单的实例,让您了解到MVP的模式的使用:

一:MVP的工作原理流程示意图:

 

Android MVP简单的实例,让您了解到MVP的模式的使用:

一:MVP的工作原理流程示意图:

 

整个流程是按照从1-->2--->3--->4的过程进行的,与MVC的区别在于,M层与V层完全分离,这样使得耦合性更加的降低,使得书写的代码更具可拓展和易改性。
说明: 1-->UI实现View的方法,引用Presenter;

              2-->Presenter调用Model,走Model具体的逻辑;

              3-->Model层具体的逻辑的实现,一般这里是做网络请求数据这块,利用回调接口,回调给Presenter方法

              4-->Presenter回调给View,即回到UI,回到View里的方法;

简单的请求实例:


 自己封装的 Volley 框架来实现简单的数据请求;

 MVP中的V层:  


            1.View层的接口来处理业务:一般定义四个方法

      public interface UIView{

           void getData(T object);//这里的参数是获取网络请求后拿到的json格式的数据对象

           void showProgress();//请求数据时,加载数据的处理

            void hideProgress();//加载数据完毕或者加载数据异常时处理

           void showException(Exception  e ,String msg);//一般处理加载异常时界面的提示


}

   2.View层的activty或fragment中去实现View的接口,并且引用Presenter

      public class MainActivty extends Activty  implents UIView{

              private ProgressBar  mBar;

             private TextView txt;

            private MainPresenter mainPresenter;

           void  onCreate(Bundle saveInstanceState)      {

                .......省略

          mainPresenter = new MainPresenter(this);

           //这里是模拟加载数据,制造一个延迟的效果

        new Handle().postDelayed(new Runable(){

          void run(){

            mainPresnter.loadData();//加载网络请求后得到数据后回调给p层的方法


}

//实现UIView接口的四个方法

  void  getData( T object){

   //将数据显示在界面上即可


}

void showProgress(){

  mBar.setVisibility(View.VISIBLE);


}

void hideProgress(){

  mBar.setVisibility(View.GONE);


}

void showException(Exception e,String msg){

//处理界面异常


}


//重写onDestory的方法,目的是调用P层的方法

void onDestory(){

mainPresetenr.detachView();

}  },2000);


}  


}

二.MVP中P层:

    P层中的接口:与View层链接

   public interface Presenter<V>{

    void onAttchView(V view);

    void onDetachView();

}

//与M层请求网络数据成功或失败时 的回调接口

   public interface IMainPresnter{

    void onSucess(T object);

    void onFailuer(Exception e,String msg);

}

  P层中的实现方法:

  public class MainPresenter implents Presenter<UIView>,IMainPresenter{

      private   UIView view;

     private MainModel model;

   public MainPresenter(UIView view){

     onAttachView(view);

    model = new MainModel(this);

}

//实现链接V层接口中的方法

   void  onAttachView(UIView view){

  this.view = view;

}

  vodi onDetachView(){

    this.view = null;

}

//实现链接M层接口的回调给P层,P层再回调给V层

  void onSucess(T object){

     view.getData(object);

     view.hideProgress();

}

 void onFailuer(Exception  e,String msg){

   view.showException(e,msg);

  view.hideProgress();

}

//开始View层调用P层,再调用M层进行网络数据的请求

  public void loadData(){

   view.showProgress();

  model.loadData();

}

}

三.MVP中M层:

  public  class MainModel {

   private IMainPresenter presenter;

  public MainModel(IMainPresneter presenter){

   this.presenter = presnter;

}

 //实现网络数据的请求方法

 public void loadData(){

//这里就是处理请求数据成功或失败后的回到,这里的volley封装的方法就大概写一下

   NetHelp help = new NetHelper();

   help.setNetWorker(new CallBack(){

   void onResponse(Response response){

    //成功

  presenter.onSucess(json格式的对象);

};

void onFailuer(){

  //失败回调

  presenter.onFilauer(e,msg);

}

}){

help.doHttpGet();

}

}

}

总结:

 MVC模式:

  视图(View):用户界面;

 控制器(Controller):业务逻辑;

 模型(Model):数据保存;

Controller完成业务逻辑后,要求model改变状态,model将新的数据发送到View,用户得到反馈。

MVVM模式:

 将MVP中的Presenter层改名为ViewModel,基本上与MVP模式完全一致。唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反应到ViewMode上,反之亦然

整个流程是按照从1-->2--->3--->4的过程进行的,与MVC的区别在于,M层与V层完全分离,这样使得耦合性更加的降低,使得书写的代码更具可拓展和易改性。
说明: 1-->UI实现View的方法,引用Presenter;

              2-->Presenter调用Model,走Model具体的逻辑;

              3-->Model层具体的逻辑的实现,一般这里是做网络请求数据这块,利用回调接口,回调给Presenter方法

              4-->Presenter回调给View,即回到UI,回到View里的方法;

简单的请求实例:


 自己封装的 Volley 框架来实现简单的数据请求;

 MVP中的V层:  


            1.View层的接口来处理业务:一般定义四个方法

      public interface UIView{

           void getData(T object);//这里的参数是获取网络请求后拿到的json格式的数据对象

           void showProgress();//请求数据时,加载数据的处理

            void hideProgress();//加载数据完毕或者加载数据异常时处理

           void showException(Exception  e ,String msg);//一般处理加载异常时界面的提示


}

   2.View层的activty或fragment中去实现View的接口,并且引用Presenter

      public class MainActivty extends Activty  implents UIView{

              private ProgressBar  mBar;

             private TextView txt;

            private MainPresenter mainPresenter;

           void  onCreate(Bundle saveInstanceState)      {

                .......省略

          mainPresenter = new MainPresenter(this);

           //这里是模拟加载数据,制造一个延迟的效果

        new Handle().postDelayed(new Runable(){

          void run(){

            mainPresnter.loadData();//加载网络请求后得到数据后回调给p层的方法


}

//实现UIView接口的四个方法

  void  getData( T object){

   //将数据显示在界面上即可


}

void showProgress(){

  mBar.setVisibility(View.VISIBLE);


}

void hideProgress(){

  mBar.setVisibility(View.GONE);


}

void showException(Exception e,String msg){

//处理界面异常


}


//重写onDestory的方法,目的是调用P层的方法

void onDestory(){

mainPresetenr.detachView();

}  },2000);


}  


}

二.MVP中P层:

    P层中的接口:与View层链接

   public interface Presenter<V>{

    void onAttchView(V view);

    void onDetachView();

}

//与M层请求网络数据成功或失败时 的回调接口

   public interface IMainPresnter{

    void onSucess(T object);

    void onFailuer(Exception e,String msg);

}

  P层中的实现方法:

  public class MainPresenter implents Presenter<UIView>,IMainPresenter{

      private   UIView view;

     private MainModel model;

   public MainPresenter(UIView view){

     onAttachView(view);

    model = new MainModel(this);

}

//实现链接V层接口中的方法

   void  onAttachView(UIView view){

  this.view = view;

}

  vodi onDetachView(){

    this.view = null;

}

//实现链接M层接口的回调给P层,P层再回调给V层

  void onSucess(T object){

     view.getData(object);

     view.hideProgress();

}

 void onFailuer(Exception  e,String msg){

   view.showException(e,msg);

  view.hideProgress();

}

//开始View层调用P层,再调用M层进行网络数据的请求

  public void loadData(){

   view.showProgress();

  model.loadData();

}

}

三.MVP中M层:

  public  class MainModel {

   private IMainPresenter presenter;

  public MainModel(IMainPresneter presenter){

   this.presenter = presnter;

}

 //实现网络数据的请求方法

 public void loadData(){

//这里就是处理请求数据成功或失败后的回到,这里的volley封装的方法就大概写一下

   NetHelp help = new NetHelper();

   help.setNetWorker(new CallBack(){

   void onResponse(Response response){

    //成功

  presenter.onSucess(json格式的对象);

};

void onFailuer(){

  //失败回调

  presenter.onFilauer(e,msg);

}

}){

help.doHttpGet();

}

}

}

总结:

 MVC模式:

  视图(View):用户界面;

 控制器(Controller):业务逻辑;

 模型(Model):数据保存;

Controller完成业务逻辑后,要求model改变状态,model将新的数据发送到View,用户得到反馈。

MVVM模式:

 将MVP中的Presenter层改名为ViewModel,基本上与MVP模式完全一致。唯一的区别是,它采用双向绑定(data-binding):View的变动,自动反应到ViewMode上,反之亦然
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mvp android