MVP 全称:Model-View-Presenter。主要逻辑如下:
MVP 全称:Model-View-Presenter。主要逻辑如下:
View。也就是Android中的Activity。同时需要创建一个View的抽象接口View interface。需要View实现的接口,View通过View interface与Presenter进行交互,降低耦合。
Model。用来操做实际数据(譬如数据存储等)。有时也需要创建一个Model的抽象接口Model interface用来降低耦合。
Presenter。作为View与Model交互的中间纽带,处理与用户交互的负责逻辑。
优点如下:(1)降低耦合度
(2)模块职责划分明显
(3)利于测试驱动开发
(4)代码复用
(5)隐藏数据
(6)代码灵活性
缺点: 需要增加很多类。
但是相比优点而言,缺点可以忽略。
具体的demo代码如下:
MainActivity:
public class MainActivity extends Activity implements IUserView,
OnClickListener {
private TextView showView;
private EditText username_et;
private EditText password_et;
private Button login_btn;
private Button getuserinfo_btn;
private Presenter pre;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bindView();
setListener();
}
private void setListener() {
login_btn.setOnClickListener(this);
getuserinfo_btn.setOnClickListener(this);
}
private void bindView() {
showView = (TextView) this.findViewById(id.showView);
username_et = (EditText) this.findViewById(id.username_et);
password_et = (EditText) this.findViewById(id.password_et);
login_btn = (Button) this.findViewById(id.login_btn);
getuserinfo_btn = (Button) this.findViewById(id.getuserinfo_btn);
pre = new Presenter(this);
}
@Override
public void setUser(UserInfo info) {
showView.setText("用户名为:" + info.getUsername() + ",密码为:"
+ info.getPassword());
}
@Override
public UserInfo getUser() {
UserInfo info = new UserInfo();
String password = password_et.getText().toString().trim();
String username = username_et.getText().toString().trim();
if (TextUtils.isEmpty(username)) {
toast("用户名不能为空");
return null;
}
if (TextUtils.isEmpty(password)) {
toast("密码不能为空");
return null;
}
info.setPassword(password);
info.setUsername(username);
return info;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.login_btn:
pre.saveInfo(getUser());
break;
case id.getuserinfo_btn:
pre.getUserInfo();
break;
default:
break;
}
}
private void toast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
}
实体bean:
创建UserInfo 实体类
public class UserInfo {
private String username;
private String 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;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return username + ":" + password;
}
}
Model 抽象层
public interface UserModel {
public UserInfo getUserInfo();
public void setUserInfo(UserInfo info);
}
Model具体实现层:
public class UserModelImp implements UserModel {
UserInfo info = new UserInfo();
@Override
public UserInfo getUserInfo() {
return info;
}
@Override
public void setUserInfo(UserInfo info) {
this.info = info;
}
}
数据交互实现层:
public class Presenter {
private UserModel userModel;
private IUserView IuserView;
public Presenter(IUserView userView) {
this.IuserView = userView;
userModel = new UserModelImp();
}
public void saveInfo(UserInfo info) {
userModel.setUserInfo(info);
}
public void getUserInfo() {
IuserView.setUser(userModel.getUserInfo());
}
}
View数据抽象层:
public interface IUserView {
public void setUser(UserInfo info);
public UserInfo getUser();
}
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。