Android development framework --MVP (personally think that the most suitable newcomers understand the MVP of the article)

Brief introduction

MVP is a derivative version of MVC, MVC with a similar, but more applicable in Android, can be divided into three layers:

MVP

Model: search for additions and deletions to the data, but also includes some data objects 
View: receiving interface for displaying a user operation, the Android View which is usually Actvitiy, Fragment. 
Presenter: with Model View is the "middleman", after receiving a request View of acquiring data to the View from the Model.

MVP&MVC

MVC tradition has a long history, but Android was chosen MVP, this is no accident. 
In MVC: 
M: solve what to render 
V: how to solve the rendering 
C: resolve user input events 

MVC2MVP

在Android中,假如使用MVC,代表V的layout资源并不能完全解决怎么去渲染的问题,还需要Activity的辅助,所以Activity也必然要代表V;但是同时,Activity一方面拥有生命周期回调,另外一方面还为View设置监听,Activity接收来自用户的输入,所以Activity也必然也代表C,Activity就像一个万能对象同时代表着V与C。因此,使用MVC并不能很好地将V与C分离开来。 
与MVC不同的是,MVP中的View是可以接收用户输入,同时也能解决怎么去渲染的问题,所以Activity可以作为MVP里面的View,但是却做不了MVC里面的View。因此,MVP更适用于Android。 
但是View的改变不只在此:View在一方面增加了接收事件的责任,又在另一方面减少了操作Model的责任。View不再直接操作Model,只能通过Presenter去Model操作数据。可以说,MVC中的View与Control交换了部分工作,就成了现在的MVP。

实现

下面的例子是一个按下按钮后在TextView显示”helloworld from presenter“的例子

//View的接口
public interface IView{    //1    
    void setData(String data);
}
//View的实现
public class View extends Activity implements IView{
    private IPresenter presenter;
    private Button button;
    private TextView text;
    public void onCreate(Bundle args){
        ....
        presenter=new Presenter(this); //2 Presenter初始化
        presenter.onCreate();   //3 将生命周期回调传给Presenter
        button.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                presenter.performOnClick(); //4 用户输入

            }
        });
    }

    @override
    public void setData(String data){   
        runOnUiThread(new Runnable(){     //ugly
                public void run(){
                    text.setText(data);
                }
            }
        );
    }
} 

以上MVP中View层的实现。 
(1)上面代码1处为View定制接口,接口中是给View设置数据的setData(String data)方法。View虽然不直接操作Model,但是这并不意味着View不跟数据打交道,相反,View需要数据来渲染自己。 
(2)代码2位置初始化Presenter,Presenter模块是在View中初始化的,同时View还将自己传给Presenter。假设不给View,Presenter写对应的接口,Presenter将依赖View,View也将依赖Presenter,双向依赖是一种错误的设计,所以View,Presenter都有对应的接口,实现依赖倒置。 
(3,4)在View中有两种情况需要调用Presenter,一种如代码3位置,View的生命周期回调中使用Presenter;另外一种如代码4位置,用户输入时使用Presenter。

//Presenter的接口
public interface IPresenter{  //5 
    void onCreate();
    void performOnClick();
}
//Presenter的实现
public class Presenter implements IPresenter{
    private IView view;   //6 拥有View与Model
    private IModel model; 

    public Presenter(IView view){   
        this.view=view;
        model=new Model();
    }
    @override
    public void onCreate(){
        ...
    }
    @override
    public void performOnClick(){
        model.getData(new ICallback(){    //7  传接口给Model
            public void onResult(String data){
                String dataFromPresenter=data+" from presenter"; //8 加工数据
                view.setData(dataFromPresenter);
            }
        });     
    }
}

以上是Presenter层的实现 
(5)代码5位置实现了Presenter接口,Presenter接口里面就是上文说的生命周期回调和用户输入,生命周期回调里面包含了对Presenter中资源初始化和释放;用户操作后,Presenter将数据加工后给View 
(6)代码6位置可以知道Presenter不止依赖了View接口,也依赖了Model接口,可以说Presenter就像中介者模式中的Mediator 
(7)代码7可以知道Presenter从Model获取数据,通常是用观察者模式来获取,因为获取数据一般是耗时操作,无法直接返回数据。 
(8)代码8可以知道Presenter并不只是做数据的中转站,更重要的是Presenter还做数据的加工。让原始数据适合View的需要,这时候Presenter又有点像适配器模式中的adapter了,只是adapter是接口转换,Presenter这里是数据转换或者实体转换。

//Model接口
public interface IModel{            //9 内嵌ICallback接口
    void getData(ICallback callback);
    public interface ICallback{    
        public void onResult(String data);
    }
}
//Model实现
public class Model implements IModel{
    public void getData(final ICallback callback){
        execute(new Runnable(){     
            public void run(){             //ugly
                ... //这里是耗时操作
                callback.onResult("hello world");    //10 返回数据
            }
        };
    }
}

以上是Model层的代码, 
(9)中不止定义了Model的接口,还定义了回调接口 
(10)中经过了一系列耗时操作,最终回调callback,之后”hello world”数据传到Presenter,经过Presenter加工再传给View

让我们在回顾一下整个流程:


Throughout the process, you need to note that View, Presenter, Model is created out how to create out where and how the interaction between each layer, which interfaces need to be defined, respectively. If you do not know, please look back earlier.
--------------------- 
Author: ancient bell 
Source: CSDN 
Original: https: //blog.csdn.net/duo2005duo/article/details/50594757 
copyright notice : This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/qq_27981847/article/details/91493368
MVP