Android如何从实现到封装一个MVP详解
作者:超神的菠萝 时间:2023-02-12 10:44:40
前言
MVP 是从经典的模式MVC演变而来,它们的基本思想有相通的地方:Controller/Presenter负责逻辑的处理,Model提供数据,View负 责显示。下面这篇文章主要给大家介绍了关于Android从实现到封装MVP的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
MVP之间的联系
大概简单的解释就是M->module处理数据,V->Act显示界面,P->M和V沟通的渠道,即P用来将数据和界面联系到一起,这样子界面和数据就可以完全独立开来,Act只做界面相关的事情,Module只处理数据,P只负责两者沟通从而实现解耦。
简单的实现一个没有任何封装的MVP
以登录界面为例子,它需要的文件大概是以下
ILoginView LoginPresenter需要和LoginAct互动的方法,比如说presenter需要获得通过act登录框的姓名,而act需要presenter处理登录数据后的返回值code
public interface ILoginView {
String getUserName();
String getUserPwd();
void onSuccess(String code);
}
IRequestLoginLoginPresenter需要和LoginModule互动的方法,比如说presenter需要通过module获取登录结果,而module需要presenter传递给他username和pwd,并传递一个实例化好的接口过去用来回调返回值
public interface IRequestLogin {
void toLogin(String userName, String pwd, IRequestResult requestResult);
}
IRequestResultLoginPresenter需要实时获取LoginModule的返回结果
public interface IRequestResult {
void onSuccess(String result);
void onFailed(String result);
}
3个管道定义好后就可以去实现3个MVP的主题module,view,presenter
LoginAct实现自ILoginView接口,并持有present的对象,从而实现LoginPresenter和它的交互,可以看到act并没有任何处理数据的地方,他要做的只是提供姓名和密码给presenter,通过present去登录,并且接收presenter处理后的返回值code。
public class LoginAct extends AppCompatActivity implements ILoginView {
private static final String TAG = "LoginAct";
LoginPresenter loginPresenter = new LoginPresenter();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
loginPresenter.tologin(this);
}
@Override
public String getUserName() {
return "userName";
}
@Override
public String getUserPwd() {
return "userPwd";
}
@Override
public void onSuccess(String code) {
Log.e(TAG, code);
}
}
LoginModule只用来做数据的处理,例如通过presenter传递过来的name和pwd去请求服务器从而获取code,并通过接口返回给presenter
public class LoginModule implements IRequestLogin{
@Override
public void toLogin(String name, String pwd, IRequestResult requestResult) {
requestResult.onSuccess("success->"+name+" "+pwd);
}
}
LoginPresenter用来做两者沟通的桥梁,他持有2个对象一个module一个view可以看出来他是中间件,用来操作module和view让他们之间可以联系到一起,当act发起登录操作时,他通过view获取到name和pwd并通过module去请求服务器拿到返回值,之后又传递给act。
public class LoginPresenter implements IRequestResult {
private LoginModule loginModule;
private ILoginView loginView;
public void tologin(LoginAct loginAct) {
loginView = loginAct;
loginModule = new LoginModule();
loginModule.toLogin(loginView.getUserName(), loginView.getUserName(), this);
}
@Override
public void onSuccess(String result) {
loginView.onSuccess(result);
}
@Override
public void onFailed(String result) {
}
}
简单的实现后,进行封装Base
看到3个接口不见了。。因为将他们放在了BaseContract文件中,比较省事。。。contract(契约类)名字自己想一个就好。。不用那么认真。。
BaseContract虽然没有什么共有的因素,还是留一个base,万一以后有需求。。IBaseModule用来实现所有处理数据中的共同点,例如所有的module都要请求数据返回一个string
public class BaseContract {
public interface IBaseModule {
}
public interface IBasePresenter {
}
public interface IBaseView {
void showToast(String msg);
}
}
BaseAct用来封装act共有的属性,例如所有的act都会实现一个IBaseView接口,并且都会持有一个presenter对象,在oncreate中实例化presenter,并且实例化presenter中的module和view
public abstract class BaseAct<V extends BaseContract.IBaseView,P extends BasePresenter> extends AppCompatActivity implements BaseContract.IBaseView {
public P presenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
presenter = initPresenter();
if (presenter != null) {
presenter.attatchWindow(initModule(),this);
}
initView();
}
protected abstract void initView();
protected abstract P initPresenter();
protected abstract BaseModule initModule();
@Override
protected void onDestroy() {
super.onDestroy();
presenter.detachWindow();
}
}
BaseModule不做任何处理
public class BaseModule implements BaseContract.IBaseModule {
}
BasePresenter同时持有module和view,在actdestory的时候释放
public class BasePresenter<M extends BaseModule, V extends BaseContract.IBaseView> {
public M module;
public V view;
void attatchWindow(M m, V v) {
this.module = m;
this.view = v;
}
void detachWindow() {
this.module = null;
this.view = null;
}
}
使用Base之后的Act
LoginContract和之前一样,定义需要交互的数据
public class LoginContract {
interface ILoginView extends BaseContract.IBaseView {
String getName();
String getPwd();
void onLoginResult(String code);
}
public interface ILoginPresenter extends BaseContract.IBasePresenter {
void getResult(LoginBean bean);
}
public interface ILoginModule extends BaseContract.IBaseModule {
void toLogin(String name, String pwd, ILoginPresenter iLoginPresenter);
}
}
LoginAct 省去了初始化presenter的步骤
public class LoginAct extends BaseAct<LoginContext.ILoginView, LoginPresenter> implements LoginContext.ILoginView {
private static final String TAG = "LoginAct";
@Override
public void showToast(String msg) {
}
@Override
protected void onResume() {
super.onResume();
presenter.toLogin();
}
@Override
protected BaseModule initModule() {
return new LoginModule();
}
@Override
protected void initView() {
}
@Override
protected LoginPresenter initPresenter() {
return new LoginPresenter();
}
@Override
public String getName() {
return "name";
}
@Override
public String getPwd() {
return "pwd";
}
@Override
public void onLoginResult(String code) {
Log.e(TAG, code);
}
}
LoginModule请求数据返回结果,差别不大
public class LoginModule extends BaseModule implements LoginContract.ILoginModule {
@Override
public void toLogin(String name, String pwd, LoginContract.ILoginPresenter iLoginPresenter) {
//网络操作,返回数据
LoginBean loginBean = new LoginBean();
loginBean.setCode(name + pwd);
iLoginPresenter.getResult(loginBean);
}
}
LoginPresenter省去实例化module和view的步骤
public class LoginPresenter extends BasePresenter<LoginModule, LoginAct> implements LoginContract.ILoginPresenter {
public void toLogin() {
module.toLogin(view.getName(), view.getPwd(), this);
}
@Override
public void getResult(LoginBean bean) {
view.onLoginResult(bean.getCode());
}
}
综上就是从实现到封装一个mvp的简易过程。。。但是优化的道路还很漫长。。
来源:http://www.jianshu.com/p/543c2bec5183
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android高级动画篇之SVG矢量动画范例
![](https://img.aspxhome.com/file/2023/0/139010_0s.gif)
WPF实现3D翻牌式倒计时特效
![](https://img.aspxhome.com/file/2023/0/87050_0s.gif)
用C#来解析PDF文件
![](https://img.aspxhome.com/file/2023/8/82238_0s.png)
Java版画板的实现方法
![](https://img.aspxhome.com/file/2023/8/64948_0s.jpg)
Java中两种基本的输入方式小结
![](https://img.aspxhome.com/file/2023/2/71642_0s.jpg)
springboot中bean的加载顺序问题
![](https://img.aspxhome.com/file/2023/0/62510_0s.png)
springboot以FTP方式上传文件到远程服务器
Java 如何实现一个http服务器
![](https://img.aspxhome.com/file/2023/2/61292_0s.png)
springboot连接不上redis的三种解决办法
![](https://img.aspxhome.com/file/2023/3/67093_0s.png)
打造酷炫的AndroidStudio插件
![](https://img.aspxhome.com/file/2023/1/138471_0s.gif)
带你了解Spring中bean的获取
![](https://img.aspxhome.com/file/2023/2/73052_0s.png)
java中LinkedList使用迭代器优化移除批量元素原理
![](https://img.aspxhome.com/file/2023/0/66180_0s.png)
IntelliJ IDEA修改新建文件自动生成注释的user名
![](https://img.aspxhome.com/file/2023/8/69308_0s.png)
Java和C++通过new创建的对象有何区别?
![](https://img.aspxhome.com/file/2023/9/67019_0s.png)
c#发送请求访问外部接口的实例
C#(int)中Convert、Parse、TryParse的区别
Spring Boot2发布调用REST服务实现方法
![](https://img.aspxhome.com/file/2023/1/71141_0s.png)
Android性能优化之利用强大的LeakCanary检测内存泄漏及解决办法
![](https://img.aspxhome.com/file/2023/9/98669_0s.jpg)
Android引入OpenCV的示例
![](https://img.aspxhome.com/file/2023/6/139376_0s.jpg)