android MVP設計模式!,androidmvp設計模式

來源:互聯網
上載者:User

android MVP設計模式!,androidmvp設計模式

實現原理:

MainActivity 用來更新UI,和顯示商務邏輯的結果!

LoginPresenterCompl 用來處理 商務邏輯

ILoginPresenter 業務處理類抽象出來的介面

ILoginView activity抽象出來的介面

1.為什麼要把activity的UI更新方法抽象出來?

因為你的項目不可能只有一個activity吧,如果想要每個activity都用MVP模式,那麼就把共有的方法抽象出來就可以的

2.為什麼要把業務處理類抽象出來?

同理,每個activity的業務處理邏輯肯定是不一樣的,我們可以使用java的特性,即重寫,雖然每個業務類的方法名,參數,傳回型別相同,但是我們可以在裡面處理不同的邏輯

比如A在裡面處理吃西瓜,B在裡面處理吃梨子,返回的都是水果類型!

3.為什麼要用MVP模式?

當你的代碼量很多的時候,如果不採用較好的架構,後續維護和尋找就會顯得很亂,自己都不知道從來找,出現問題第一時間也沒辦法分析出來,總之一個好的APK,最好是在前期採用好的架構搭建!

首先是activity的代碼:

ublic class MainActivity extends Activity implements ILoginView{

private Button button1;
private Button button2;
private EditText edit1;
private EditText edit2;
private ILoginPresenter presenterCompl;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
presenterCompl = new LoginPresenterCompl(this);
button1 = (Button)findViewById(R.id.button1);
button2 = (Button)findViewById(R.id.button2);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);

button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
presenterCompl.doLogin(edit1.getText().toString(), edit2.getText().toString());
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
presenterCompl.clear();
}
});

}
@Override
public void onClearText() {
edit1.setText("");
edit2.setText("");
Toast.makeText(getApplicationContext(), "clear", 1).show();
}

@Override
public void onLoginResult(Boolean result, int code) {
if(result){
Toast.makeText(getApplicationContext(), "登入成功", 1).show();
}else{
Toast.makeText(getApplicationContext(), "登入失敗", 1).show();
}
}

}

他實現了ILoginView介面,這個介面定義的兩個方法,一個用來清除edittext的資料,一個用來顯示邏輯任務類的處理結果,結果的方法可以根據自己公司具體業務需求來定!

public interface ILoginView {
public void onClearText();
public void onLoginResult(Boolean result, int code);
}

接下來是邏輯任務類LoginPresenterCompl

public class LoginPresenterCompl implements ILoginPresenter{
private ILoginView mView;
private User user;
@Override
public void clear() {
mView.onClearText();
}

@Override
public void doLogin(String name, String password) {
boolean result = false;
int code = 0;
if(name.equals(user.getName())&&password.equals(user.getPassword())){
result = true;
code = 1;
}else{
result = false;
code = 0;
}
mView.onLoginResult(result, code);
}
public LoginPresenterCompl(ILoginView view) {
this.mView = view;
user = new User("000","123456");
}
}

這個類實現了ILoginPresenter介面,因為你以後項目的邏輯任務肯定不止一種情況,所以我們抽象出來共同的方法

只要實現這個介面就行,具體的任務邏輯看自己公司需要,比如我們公司採用的登入驗證,在activity中獲得使用者輸入

的帳號和密碼,然後跟實體類的資料進行對比(注意這裡的實體類是為了方便才這樣寫的,實際情況應該是從資料庫中

或者從網路請求中擷取資料然後儲存到實體類,這裡就不具體說了),最後調用activity裡面抽象出來的更新UI的方法,

實際上就是把結果返回給activity的,因為activity實現了這個這個介面!

public interface ILoginPresenter {
public void clear();
public void doLogin(String name, String password);
}

實體類:

public class User {
private String name;
private String password;
public User(String name,String pwd) {
this.name = name;
this.password = pwd;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [name=" + name + ", password=" + password + "]";
}

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.