溫馨提示×

溫馨提示×

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

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

Android從實現(xiàn)到封裝一個MVP的示例

發(fā)布時間:2021-02-20 11:06:00 來源:億速云 閱讀:129 作者:小新 欄目:移動開發(fā)

這篇文章主要介紹了Android從實現(xiàn)到封裝一個MVP的示例,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

前言

MVP 是從經(jīng)典的模式MVC演變而來,它們的基本思想有相通的地方:Controller/Presenter負責(zé)邏輯的處理,Model提供數(shù)據(jù),View負 責(zé)顯示。下面這篇文章主要給大家介紹了關(guān)于Android從實現(xiàn)到封裝MVP的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細的介紹吧。

MVP之間的聯(lián)系

大概簡單的解釋就是M->module處理數(shù)據(jù),V->Act顯示界面,P->M和V溝通的渠道,即P用來將數(shù)據(jù)和界面聯(lián)系到一起,這樣子界面和數(shù)據(jù)就可以完全獨立開來,Act只做界面相關(guān)的事情,Module只處理數(shù)據(jù),P只負責(zé)兩者溝通從而實現(xiàn)解耦。

簡單的實現(xiàn)一個沒有任何封裝的MVP

以登錄界面為例子,它需要的文件大概是以下

Android從實現(xiàn)到封裝一個MVP的示例

ILoginView LoginPresenter需要和LoginAct互動的方法,比如說presenter需要獲得通過act登錄框的姓名,而act需要presenter處理登錄數(shù)據(jù)后的返回值code

public interface ILoginView {
 String getUserName();
 String getUserPwd();
 void onSuccess(String code);
}

IRequestLoginLoginPresenter需要和LoginModule互動的方法,比如說presenter需要通過module獲取登錄結(jié)果,而module需要presenter傳遞給他username和pwd,并傳遞一個實例化好的接口過去用來回調(diào)返回值

public interface IRequestLogin {
 void toLogin(String userName, String pwd, IRequestResult requestResult);
}

IRequestResultLoginPresenter需要實時獲取LoginModule的返回結(jié)果

public interface IRequestResult {

 void onSuccess(String result);

 void onFailed(String result);

}

3個管道定義好后就可以去實現(xiàn)3個MVP的主題module,view,presenter

LoginAct實現(xiàn)自ILoginView接口,并持有present的對象,從而實現(xiàn)LoginPresenter和它的交互,可以看到act并沒有任何處理數(shù)據(jù)的地方,他要做的只是提供姓名和密碼給presenter,通過present去登錄,并且接收presenter處理后的返回值code。

public class LoginAct extends AppCompatActivity implements ILoginView {

 private static final String TAG = "LoginAct";
 LoginPresenter loginPresenter = new LoginPresenter();

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  loginPresenter.tologin(this);
 }

 @Override
 public String getUserName() {
  return "userName";
 }

 @Override
 public String getUserPwd() {
  return "userPwd";
 }

 @Override
 public void onSuccess(String code) {
  Log.e(TAG, code);
 }

}

LoginModule只用來做數(shù)據(jù)的處理,例如通過presenter傳遞過來的name和pwd去請求服務(wù)器從而獲取code,并通過接口返回給presenter

public class LoginModule implements IRequestLogin{

 @Override
 public void toLogin(String name, String pwd, IRequestResult requestResult) {
  requestResult.onSuccess("success->"+name+" "+pwd);
 }

}

LoginPresenter用來做兩者溝通的橋梁,他持有2個對象一個module一個view可以看出來他是中間件,用來操作module和view讓他們之間可以聯(lián)系到一起,當(dāng)act發(fā)起登錄操作時,他通過view獲取到name和pwd并通過module去請求服務(wù)器拿到返回值,之后又傳遞給act。

public class LoginPresenter implements IRequestResult {
 private LoginModule loginModule;
 private ILoginView loginView;

 public void tologin(LoginAct loginAct) {
  loginView = loginAct;
  loginModule = new LoginModule();
  loginModule.toLogin(loginView.getUserName(), loginView.getUserName(), this);
 }

 @Override
 public void onSuccess(String result) {
  loginView.onSuccess(result);
 }

 @Override
 public void onFailed(String result) {

 }

}

簡單的實現(xiàn)后,進行封裝Base

Android從實現(xiàn)到封裝一個MVP的示例

看到3個接口不見了。。因為將他們放在了BaseContract文件中,比較省事。。。contract(契約類)名字自己想一個就好。。不用那么認真。。

BaseContract雖然沒有什么共有的因素,還是留一個base,萬一以后有需求。。IBaseModule用來實現(xiàn)所有處理數(shù)據(jù)中的共同點,例如所有的module都要請求數(shù)據(jù)返回一個string

public class BaseContract {
 public interface IBaseModule {
 }

 public interface IBasePresenter {
 }

 public interface IBaseView {
  void showToast(String msg);
 }
}

BaseAct用來封裝act共有的屬性,例如所有的act都會實現(xiàn)一個IBaseView接口,并且都會持有一個presenter對象,在oncreate中實例化presenter,并且實例化presenter中的module和view

public abstract class BaseAct<V extends BaseContract.IBaseView,P extends BasePresenter> extends AppCompatActivity implements BaseContract.IBaseView {

 public P presenter;

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  presenter = initPresenter();

  if (presenter != null) {
   presenter.attatchWindow(initModule(),this);
  }

  initView();

 }

 protected abstract void initView();

 protected abstract P initPresenter();

 protected abstract BaseModule initModule();

 @Override
 protected void onDestroy() {
  super.onDestroy();
  presenter.detachWindow();
 }
}

BaseModule不做任何處理

public class BaseModule implements BaseContract.IBaseModule {
}

BasePresenter同時持有module和view,在actdestory的時候釋放

public class BasePresenter<M extends BaseModule, V extends BaseContract.IBaseView> {

 public M module;

 public V view;

 void attatchWindow(M m, V v) {
  this.module = m;
  this.view = v;
 }

 void detachWindow() {
  this.module = null;
  this.view = null;
 }

}

使用Base之后的Act

LoginContract和之前一樣,定義需要交互的數(shù)據(jù)

public class LoginContract {

 interface ILoginView extends BaseContract.IBaseView {

  String getName();

  String getPwd();

  void onLoginResult(String code);

 }

 public interface ILoginPresenter extends BaseContract.IBasePresenter {
  void getResult(LoginBean bean);
 }

 public interface ILoginModule extends BaseContract.IBaseModule {
  void toLogin(String name, String pwd, ILoginPresenter iLoginPresenter);
 }
}

LoginAct 省去了初始化presenter的步驟

public class LoginAct extends BaseAct<LoginContext.ILoginView, LoginPresenter> implements LoginContext.ILoginView {
 private static final String TAG = "LoginAct";

 @Override
 public void showToast(String msg) {

 }

 @Override
 protected void onResume() {
  super.onResume();
  presenter.toLogin();
 }

 @Override
 protected BaseModule initModule() {
  return new LoginModule();
 }

 @Override
 protected void initView() {
 }

 @Override
 protected LoginPresenter initPresenter() {
  return new LoginPresenter();
 }

 @Override
 public String getName() {
  return "name";
 }

 @Override
 public String getPwd() {
  return "pwd";
 }

 @Override
 public void onLoginResult(String code) {
  Log.e(TAG, code);

 }
}

LoginModule請求數(shù)據(jù)返回結(jié)果,差別不大

public class LoginModule extends BaseModule implements LoginContract.ILoginModule {

 @Override
 public void toLogin(String name, String pwd, LoginContract.ILoginPresenter iLoginPresenter) {
  //網(wǎng)絡(luò)操作,返回數(shù)據(jù)
  LoginBean loginBean = new LoginBean();
  loginBean.setCode(name + pwd);
  iLoginPresenter.getResult(loginBean);
 }

}

LoginPresenter省去實例化module和view的步驟

public class LoginPresenter extends BasePresenter<LoginModule, LoginAct> implements LoginContract.ILoginPresenter {

 public void toLogin() {

  module.toLogin(view.getName(), view.getPwd(), this);

 }

 @Override
 public void getResult(LoginBean bean) {
  view.onLoginResult(bean.getCode());
 }

}

感謝你能夠認真閱讀完這篇文章,希望小編分享的“Android從實現(xiàn)到封裝一個MVP的示例”這篇文章對大家有幫助,同時也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

向AI問一下細節(jié)

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

AI