您好,登錄后才能下訂單哦!
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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。