溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

android MVP 設(shè)計(jì)模式

發(fā)布時(shí)間:2020-07-17 23:00:22 來(lái)源:網(wǎng)絡(luò) 閱讀:444 作者:飛指 欄目:移動(dòng)開(kāi)發(fā)

 MVP 全稱:Model-View-Presenter。主要邏輯如下:

 MVP 全稱:Model-View-Presenter。主要邏輯如下:

  1. View。也就是Android中的Activity。同時(shí)需要?jiǎng)?chuàng)建一個(gè)View的抽象接口View interface。需要View實(shí)現(xiàn)的接口,View通過(guò)View interface與Presenter進(jìn)行交互,降低耦合。

  2. Model。用來(lái)操做實(shí)際數(shù)據(jù)(譬如數(shù)據(jù)存儲(chǔ)等)。有時(shí)也需要?jiǎng)?chuàng)建一個(gè)Model的抽象接口Model interface用來(lái)降低耦合。

  3. Presenter。作為View與Model交互的中間紐帶,處理與用戶交互的負(fù)責(zé)邏輯。  

   優(yōu)點(diǎn)如下:(1)降低耦合度

     (2)模塊職責(zé)劃分明顯

     (3)利于測(cè)試驅(qū)動(dòng)開(kāi)發(fā)

     (4)代碼復(fù)用

     (5)隱藏?cái)?shù)據(jù)

     (6)代碼靈活性
缺點(diǎn):  需要增加很多類。
  
  但是相比優(yōu)點(diǎn)而言,缺點(diǎn)可以忽略。


 具體的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();

}


}

實(shí)體bean:

創(chuàng)建UserInfo 實(shí)體類

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具體實(shí)現(xiàn)層:

public class UserModelImp implements UserModel {

UserInfo info = new UserInfo();

@Override

public UserInfo getUserInfo() {

return info;

}

@Override

public void setUserInfo(UserInfo info) {

this.info = info;

}

}

 

數(shù)據(jù)交互實(shí)現(xiàn)層:

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數(shù)據(jù)抽象層:


public interface IUserView {

public void setUser(UserInfo info);

public UserInfo getUser();

}



向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI