MVP 全稱:Model-View-Presenter。主要邏輯以下:ide
MVP 全稱:Model-View-Presenter。主要邏輯以下:
測試
View。也就是Android中的Activity。同時須要建立一個View的抽象接口View interface。須要View實現的接口,View經過View interface與Presenter進行交互,下降耦合。this
Model。用來操作實際數據(譬如數據存儲等)。有時也須要建立一個Model的抽象接口Model interface用來下降耦合。spa
Presenter。做爲View與Model交互的中間紐帶,處理與用戶交互的負責邏輯。 orm
優勢以下:(1)下降耦合度接口
(2)模塊職責劃分明顯開發
(3)利於測試驅動開發get
(4)代碼複用it
(5)隱藏數據io
(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();
}