<Android 基础(三)> MVP模式
前言
MVP,这里指的并不是篮球比赛中的MVP(最有价值球员),而是一种代码框架和设计思想,它是由MVC演变而来的。
MVP模式(Model-View-Presenter)
是MVC模式的一个衍生。主要目的是为了解耦,使项目易于维护。
Model 是业务逻辑和实体模型
View 是包含Presenter的引用。所要做的就是当有交互时,调用Presenter里的对应方法。通常会通过Activity实现
Presenter 是负责完成View于Model间的交互,从Model里取数据,返回给View处理好的数据。
MVC模式(Model-View-Controller)
主要目的是讲显示逻辑和业务逻辑独立出来
Model 是应用程序中用于处理应用程序数据逻辑的部分。
View 是应用程序中处理数据显示的部分。
Controller 是应用程序中处理用户交互的部分
从构造图上可以看到最主要的差异就是
MVC中是允许Model和View进行交互的,而MVP中很明显,Model与View之间的交互由Presenter完成。还有一点就是Presenter与View之间的交互是通过接口的
MVP使用方法
代码结构
代码内容
LoginActivity.java
public class LoginActivity extends AppCompatActivity implements ILoginView {
@Bind(R.id.usernameWrapper)
TextInputLayout usernameWrapper;
@Bind(R.id.passwordWrapper)
TextInputLayout passwordWrapper;
@Bind(R.id.bt_clear)
Button btClear;
@Bind(R.id.bt_login)
Button btLogin;
private LoginPresenterCompl loginPresenterCompl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
loginPresenterCompl = new LoginPresenterCompl(this);
}
@OnClick({R.id.bt_clear, R.id.bt_login})
public void onClick(View view) {
String username = usernameWrapper.getEditText().getText().toString();
String password = passwordWrapper.getEditText().getText().toString();
switch (view.getId()) {
case R.id.bt_clear:
loginPresenterCompl.clearText();
break;
case R.id.bt_login:
loginPresenterCompl.doLogin(username, password);
break;
}
}
@Override
public void onLoginResult(boolean isLogged) {
if (isLogged) {
Toast.makeText(this, "Logged in Successfully", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Logged Failed", Toast.LENGTH_SHORT).show();
}
return;
}
@Override
public void onClearText() {
usernameWrapper.getEditText().setText("");
passwordWrapper.getEditText().setText("");
usernameWrapper.clearFocus();
passwordWrapper.clearFocus();
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (imm != null) {
imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(),
0);
}
}
}
LoginActivity实现ILoginView接口
public interface ILoginView {
void onLoginResult(boolean isLogged);
void onClearText();
}
LoginPresenterCompl.java实现ILoginPresenter接口
public class LoginPresenterCompl implements ILoginPresenter{
private ILoginView iLoginView;
Handler handler;
public LoginPresenterCompl(ILoginView loginView) {
iLoginView = loginView;
handler = new Handler(Looper.getMainLooper());
}
@Override
public void doLogin(String username, String password) {
final String tp_username = username;
final String tp_password = password;
new Thread(new Runnable() {
@Override
public void run() {
boolean isLogged = false ;
if(UserModel.isValidUser(tp_username , tp_password)) {
isLogged = true;
}
final boolean tp_isLogged = isLogged;
handler.post(new Runnable() {
@Override
public void run() {
iLoginView.onLoginResult(tp_isLogged);
}
});
}
}).start();
}
@Override
public void clearText() {
new Thread(new Runnable() {
@Override
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
iLoginView.onClearText();
}
});
}
}).start();
}
}
public interface ILoginPresenter {
void doLogin(String username, String password);
void clearText();
}
UserModel.java这个没有怎么使用,但是实际使用过程中不同的Model会有不同的处理方式
public class UserModel {
public String username;
public String password;
public UserModel(String username, String password) {
this.username = username;
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public static boolean isValidUser(String username, String password){
return (username.equals("guagua") && password.equals("guagua"));
}
}
简约类图:
效果图
<Android 基础(三)> MVP模式的更多相关文章
- [译]Google官方关于Android架构中MVP模式的示例
概述 该示例(TODO-MVP)是后续各种示例演变的基础,它主要演示了在不带架构性框架的情况下实现M-V-P模式.其采用手动依赖注入的方式来提供本地数据源和远程数据源仓库.异步任务通过回调处理. 注意 ...
- Android MVP模式 谷歌官方代码解读
Google官方MVP Sample代码解读 关于Android程序的构架, 当前(2016.10)最流行的模式即为MVP模式, Google官方提供了Sample代码来展示这种模式的用法. Repo ...
- 在Andoid开发中使用MVP模式来解耦,增加可测试性
by Jeff Angelini posted on 7/20/2011 2:35:00 PM 将应用程序UI的表现从Ui的逻辑中分离是一个好的想法.这种分离减少了代码耦合,代码更加干净, 甚至可以有 ...
- MVP模式(Android)
以前在写项目的时候,没有过多考虑架构模式的问题,因为之前一直做J2EE开发,而J2EE都是采用MVC模式进行开发的,所以在搭建公司项目的时候,也是使用类似MVC的架构(严格来讲,之前的项目还算不上MV ...
- Android -- 思考 -- 为什么要在项目中使用MVP模式
1,其实有时候一直在找借口不去思考这个问题,总是以赶项目为由,没有很认真的思考这个问题,为什么我们要在项目中使用MVP模式,自己也用MVP也已经做了两个项目,而且在网上也看了不少的文章,但是感觉在高层 ...
- iOS学习之MVC,MVVM,MVP模式优缺点
为什么要关注架构设计? 因为假如你不关心架构,那么总有一天,需要在同一个庞大的类中调试若干复杂的事情,你会发现在这样的条件下,根本不可能在这个类中快速的找到以及有效的修改任何bug.当然,把这样的一个 ...
- MVP模式和MVVM模式
MVP模式 模型-视图-表示器,也就是MVP模式.是mvc模式的一种衍生模式,专注于改进表示逻辑. 与MVC不同,来自view的调用将委托给presenter(表示器),表示器通过接口与view对话. ...
- 控件使用经验-MVP模式+控件封装
项目背景 几年前参与了一个面向学校的人事管理软件的开发,基于WinForm平台.今天主要想谈一谈其中关于控件的使用经验.这个项目我们大量使用了第三方控件.由于这个产品的生命周期很长,我们在设计时要考虑 ...
- MVP模式在Android项目中的使用
以前在写项目的时候,没有过多考虑架构模式的问题,因为之前一直做J2EE开发,而J2EE都是采用MVC模式进行开发的,所以在搭建公司项目的时候,也是使用类似MVC的架构(严格来讲,之前的项目还算不上MV ...
- MVP模式
一.软件设计鼻祖MVC 1.1.MVC 第一次听到MVC这个名词是在C#中,相信对于MVC大家都已经很熟悉了,作为一种软件设计模式,MVC这个概念已经诞生好多年了. 如果你已经开发一段时间的iOS应用 ...
随机推荐
- 做c语言的码农专业发展方向
写了几年C语言代码,最近在思索,何去何从比较好? 搜索了一下,发现几个答案: 2015年10月编程语言排行榜 丢开C语言在教学应用外.在目前C语言的实际应用中.常见的应用的情景有如下: 内核/驱动,b ...
- 5.6 安装slack,Skype,google
1.将安装包放到自己的目录下: 2.打开终端,进入安装包所在的目录(cd /文件名) 3.输入命令:sudo apt-get install 安装包名 4.等待安装结束 5. 在搜索框中输入关键字sl ...
- es6基础系列四--字符串的拓展
1 for...of 字符串的遍历接口 for(let i of "abc"){ console.log(i); } // a // b // c 2 includes 是否包含某 ...
- 每次选中数组中的N条数据, 如果让每条数据被选中的次数做到平均??
经常有这样的需求, 有一组数据, 每次展示其中的1条或N条,希望每条数据展示量可以做到平均. 一开始想依次展示每条数据并做记录,整组数据全展示一遍之后清除记录, 然后一直循环下去. 实现的过程中又觉得 ...
- codeforces 367B
题目代码到是不难但是很难想通题目的解决方法. #include<iostream> using namespace std; ]; int main() { int n,m; while( ...
- C#----接口与抽象类
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 接口 { ...
- C# 写 LeetCode easy #28 Implement strStr()
28.Implement strStr() Implement strStr(). Return the index of the first occurrence of needle in hays ...
- Ubuntu16.4下安装Chrome浏览器以及Chromedriver
一.Chrome浏览器的安装 对于谷歌Chrome32位版本,使用如下链接: wget https://dl.google.com/linux/direct/google-chrome-stable_ ...
- InnoDB recovery过程解析
本文来自网易云社区. InnoDB如果发生意外宕机了,数据会丢么?对于这个问题,稍微了解一点MySQL知识的人,都会斩钉截铁的回答:不会!为什么?他们也会毫不犹豫的说:因为有重做日志(redo log ...
- Github网站css加载不出来的处理方法(转,亲测有效)
转载链接:https://blog.csdn.net/qq_36589706/article/details/80573008因为工作需求,自己会经常使用到github,但是突然有一天打开github ...