Presenter: serves as a bridge between views and models. After retrieving data from the Model layer, it returns the data to the View layer, but unlike the MVC structure, it also determines the interaction with the View layer.
The Data Interaction diagram is as follows:
Observe the interaction diagram of the two modes above. Is the MVP mode clearer and simpler!
(3) differences between MVC and MVP:
Let's take a look at the two comparisons in the lower part of the timeline to see the specific differences:
We can see that in MVP mode, the interaction between View and Model is completed through Presenter, so that unified management and logic are clearer.
(4) MVP mode example:
. Specific functional requirements: we use the MVP mode to implement user logon.
4. Example:
4. The project code framework is as follows:
4. Code implementation:
4.4.1.Model layer: Bean class (Entity) and PersonBean class. Then, there are logon methods in the business logic class, And the callback interface in the successful logon status is passed in as follows:
Packagecom. chinaztt. fda. entity;/*** current class annotation: User Information entity class * Project name: FastDev4Android * package name: com. chinaztt. fda. entity * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: Jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classPersonBean {private String username; private String password; public PersonBean () {} public PersonBean (String username, Stringpassword) {this. username = username; this. password = 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 () {return PersonBean {+ username = '+ username + ''' +, password = '+ password + ''' + '}';}}
public interfaceIPersonBiz { void login(String username,Stringpassword,LoginRequestCallBack valueCallBack);}
Packagecom. chinaztt. fda. biz. imp; importcom. chinaztt. fda. biz. IPersonBiz; importcom. chinaztt. fda. biz. loginRequestCallBack; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. utils. log;/*** current class comment: User-related business logic implementation class * Project name: FastDev4Android * package name: com. chinaztt. fda. biz. imp * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classPersonBizImp implements IPersonBiz {private static final String TAG = PersonBizImp; @ Override public void login (final String username, final String password, final LoginRequestCallBack valueCallBack) {Log. d (TAG, username: + username +, password: + password); new Thread (new Runnable () {@ Override public void run () {try {Thread. sleep (4500);} catch (InterruptedExceptione) {e. printStackTrace () ;}// start to log on. Here, we should request the server for data verification if (username. equals (jiangqq) & password. equals (12345) {valueCallBack. loginSuccess (new PersonBean (username, password);} else {valueCallBack. loginFailed ();}}}). start ();}}
Packagecom. chinaztt. fda. biz; importcom. chinaztt. fda. entity. personBean;/*** current class annotation: callback of login request result * Project name: FastDev4Android * package name: com. chinaztt. fda. biz * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public interfaceLoginRequestCallBack {// callback Method for Logon success void loginSuccess (PersonBean personBean); // callback Method for Logon Failure void loginFailed ();}
4.4.2.View layer: This layer interacts with the View through Presenter. Here, an interface ILoginView needs to be defined:
Packagecom. chinaztt. fda. ui. view; importcom. chinaztt. fda. entity. personBean;/*** current class annotation: Operation Interface related to logon page * Project name: FastDev4Android * package name: com. chinaztt. fda. ui. view * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public interfaceILoginView {// obtain the username String getUserName (); // obtain the password String getPassword (); void showSuccessInfo (PersonBeanpersonBean ); void showFailedInfo ();}
With the above interface, we need to write our implementation class Activity, which is very simple.
Packagecom. chinaztt. fda. test; importandroid. OS. bundle; importandroid. view. view; importandroid. widget. button; importandroid. widget. editText; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. presenter. loginPresenter; importcom. chinaztt. fda. ui. r; importcom. chinaztt. fda. ui. base. baseActivity; importcom. chinaztt. fda. ui. view. ILoginView; importcom. chinaztt. fda. utils. log; importorg. androidannotations. annotations. EActivity;/*** Note of the current class: MVP development mode instance * Project name: FastDev4Android * package name: com. chinaztt. fda. author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */@ EActivitypublic classMVPTestActivity extends BaseActivity implements ILoginView {private static final String TAG = MVPTestActivity; private EditText ed_username; private EditText ed_password; private Button secret; private parameter secret; @ Override protected void onCreate (BundlesavedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. mvp_test_layout); ed_username = (EditText) this. findViewById (R. id. ed_username); ed_password = (EditText) this. findViewById (R. id. ed_password); btn_login = (Button) this. findViewById (R. id. btn_login); mLoginPresenter = newLoginPresenter (this); btn_login.setOnClickListener (newView. onClickListener () {@ Override public void onClick (View v) {mLoginPresenter. loginSystem () ;}}) ;}/ *** returns the username information * @ return */@ Override public String getUserName () {returned_username.getText (). toString (). trim ();}/*** returns the user password information * @ return */@ Override public String getPassword () {returned_password.getText (). toString (). trim ();}/*** callback for Successful Logon * @ param personBean */@ Override public void showSuccessInfo (PersonBeanpersonBean) {Log. d (TAG, showSuccessInfo: + personBean. toString (); showToastMsgShort (LOGIN successful: + personBean. toString ();}/*** logon Failure callback */@ Override public void showFailedInfo () {Log. d (TAG, showFailedInfo ...); showToastMsgShort (logon failed ...);}}
At last, there is still one less interaction bridge Presenter:
4.4.3.Presenter layer: serves as an interactive bridge between the Model and View. In this example, perform the logon operation, log on to the Model business, and return the logon result to the View layer, this is simple:
Packagecom. chinaztt. fda. presenter; importandroid. OS. handler; importcom. chinaztt. fda. biz. IPersonBiz; importcom. chinaztt. fda. biz. loginRequestCallBack; importcom. chinaztt. fda. biz. imp. personBizImp; importcom. chinaztt. fda. entity. personBean; importcom. chinaztt. fda. ui. view. ILoginView; importcom. chinaztt. fda. utils. log;/*** current class annotation: responsible for completing the interaction between the logon interface View and Model (IPersonBiz) * Project name: FastDev4Android * package name: com. chinaztt. fda. presenter * Author: Jiang qingqing on 15/10/27 * email: [email protected] * QQ: 781931404 * company: jiangsu Zhongtian Technology Software Technology Co., Ltd. */public classLoginPresenter {private static final String TAG = LoginPresenter; private ILoginView mLoginView; private IPersonBiz mPersonBiz; private Handler mHandler = new Handler (); public LoginPresenter (ILoginView view) {mLoginView = view; mPersonBiz = new PersonBizImp ();} public void loginSystem () {mPersonBiz. login (mLoginView. getUserName (), mLoginView. getPassword (), newLoginRequestCallBack () {/*** logon successful * @ param personBean */@ Override public void loginSuccess (finalPersonBean personBean) {Log. d (TAG, logon successful: + personBean. toString (); mHandler. post (new Runnable () {@ Override public void run () {mLoginView. showSuccessInfo (personBean) ;}}) ;}/ *** Logon Failed */@ Override public void loginFailed () {Log. d (TAG, Logon Failed ...); mHandler. post (new Runnable () {@ Override public void run () {mLoginView. showFailedInfo ();;}});}});}}
At this point, our MVP mode example is basically completed. Let's take a look at the above demonstration and we will be OK.