MasterofProject

[FastDev4Android] Android framework for the development of MVP development mode detailed (nineteen)

label AndroidDevelopment modelframeMVCMVP
7610 people read comment(11) Collection report
Classification:

Reproduced please indicate the source:

Http://prog3.com/sbdm/blog/developer_jiangqq/article/details/49613861
This article from:[clear] Jiangqing blog

(One).preface:

      [news] personal website has been running on the line, behind the blog and dry goods, such as excellent articles will update, please attention:Http://www.lcode.org

Today our project continues to update,Today we mainly explainMVPDevelopment model and specific examples.

FastDev4AndroidFramework project address:Https://github.com/jiangqqlmj/FastDev4Android

(Two).brief introduction:

   MVP (ViewPresenter Model) model is a well-known MVC (ViewController Model) model of an evolutionary version, at present it is more and more important in the development of Android applications. At first glance, we'll feel like adding a lot of class interface code looks even more clear.

    MVP mode can separate the display layer and the logic layer, so how to work with the function of the function of the display can be separated, MVP mode can be ideal to achieve the same logic code collocation different display interface. But MVP is not a structured model, it is only responsible for the display layer, any time you can use the MVP model in their own project structure.(Not limited to Android project development)

    becauseMVPActually is fromMVCSchema evolution generated, then we look at the famous MVC model:

  •      View:Corresponding to the layout file
  • Model:Business logic and entity model
  •     Controller:controller,AndroidIn correspondence toActivity

   The corresponding interaction diagram is as follows:

  

   althoughAndroidSystem application development itself is followedMVCDevelopment model, but we look at the View layer andActivityAnd specific view layout file in the data binding and event handling code is redundancy in activity, so we often see can see the activity class motionless is that at least 900 line, more than a thousand even thousands of lines. So now the evolution of the upgrade version of the MVP model is how to do? MVP mode will be introduced PresEnterLayer, the model complex to complete the interaction between the View layer and the Model layer, then the specific MVP corresponds to the following:

  •  View:View is usually achieved by the Activity, it will contain a reference to View, Presenter to do just in every time there is an interface call (such as a button click) call Presenter method.
  • Model:Business logic and entity model
  •      PresEnterMainly as a bridge of communication between View and Model, it retrieves data from the Model layer, returns to the View layer, but unlike the MVC structure, as it can also determine the interaction with the View layer.

 The data exchange diagram is as follows:

Observation of the above two patterns of interaction diagram, is not a MVP model is more clear and simple ah!

(Three).MVCandMVPdifference:

     Let us look at the following two comparison, you can see the specific difference between the:

We can find out the picture aboveMVPMode, ViewandModelThe interaction is throughPresenterTo carry out the completion of such a unified management, the logic will be more clear.

(Four.MVP)Model examples to explain:

4.1.Specific implementation of functional requirements:We are usingMVPMode to achieve user login operation.

4.2.Examples are as follows:

           

4.3.The project code framework is as follows:


     


     

4.4.Code specific implementation:

4.4.1.Modellayer:Beanclass(Entity),PersonBeanClass, and then in the business logic class has a login method, while the login success of the callback interface into the callback interface, specific as follows:

Packagecom.chinaztt.fda.entity;
 
/ * *
* current class comment: user information entity class
* project name: FastDev4Android
:*包名com.chinaztt.fda.entity
*作者:江清清on/10/27 15 14 13
:*邮箱jiangqqlmj@163.com
*:781931404 QQ
*:江苏中天科技软件技术有限公司company
*/
personbean { public class
私人字符串用户名;
私人字符串密码;
 
公共personbean(){
}
 
公共personbean(串用户名,stringpassword){
this.username =用户名;
this.password =密码;
}
 
公共字符串getusername(){
返回的用户名;
}
 
public void { setusername(串用户名)
this.username =用户名;
}
 
公共字符串向GetPassword(){
返回的密码;
}
 
public void { setpassword(字符串密码)
this.password =密码;
}
 
“重写
公共字符串的toString(){
返回“personbean {“+
“用户名=用户名+“”+“+”。
”,密码为“password”\“+ + +
};
}
}
ipersonbiz { public接口
void(串用户名登录,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;
 
*
:*当前类注释用户相关业务逻辑实现类
:*项目名fastdev4android
:*包名com.chinaztt.fda.biz.imp
*作者:江清清27对15 / 10 / 16:33
:*邮箱jiangqqlmj@163.com
*:781931404 QQ
*:江苏中天科技软件技术有限公司company
*/
ipersonbiz { public class personbizimp工具
私有静态String =“personbizimp最后一天”;
“重写
public void(最终用户名登录字符串,字符串密码loginrequestcallback决赛,最终valuecallback){
log.d(天,“用户名:”+“+用户名+密码,密码:”);
新线程运行(){(New
“重写
public void run(){
try {
thread.sleep(4500);
(interruptedexceptione){ } catch
e.printstacktrace();
}
//进行开始登录,这边应该进行请求服务器,进行数据验证
if((“jiangqq username.equals password.equals(”)&“12345”)){
valuecallback.loginsuccess(新personbean(用户名,密码);
} {
valuecallback.loginfailed();
}
}
.start());}
}
}
packagecom.chinaztt.fda.biz;
 
importcom.chinaztt.fda.entity.personbean;
*
:*当前类注释登录请求结果回调
:*项目名fastdev4android
:*包名com.chinaztt.fda.biz
*作者:江清清on 15/10/27 19:50
:*邮箱jiangqqlmj@163.com
*:781931404 QQ
*:江苏中天科技软件技术有限公司company
*/
loginrequestcallback { public接口
//登录成功回调方法
loginsuccess(personbean personbean);void
//登录失败回调方法
loginfailed(void);
}

4.4.2。查看该通过主持人查看进行交互,这边需要定义一个接口ilogin查看

packagecom.chinaztt.fda.ui.view;
 
importcom.chinaztt.fda.entity.personbean;
 
*
:*当前类注释登录页面相关操作功能接口
:*项目名fastdev4android
:*包名com.chinaztt.fda.ui.view
*作者:江清清27对15 / 10 / 16:35
:*邮箱jiangqqlmj@163.com
*:781931404 QQ
*:江苏中天科技软件技术有限公司company
*/
iloginview { public接口
//获取用户名
字符串getusername();
//获取密码
弦向GetPassword();
  
showsuccessinfo(personbeanpersonbean);void
showfailedinfo(void);
}

 有了上面的接口之后,我们就需要写我们的实现类活动了,就非常简单了

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;
 
/ * *
* current class comment: instance of MVP development mode
* project name: FastDev4Android
* package name: com.chinaztt.fda.test
* Author: Jiang Qingqing 15/10/27 13:38 on
* mailbox: jiangqqlmj@163.com
* QQ: 781931404
Jiangsu Transit Technology Co., Ltd.
* /
@EActivity
Class MVPTestActivity extends BaseActivity implements public ILoginView{
Static final String TAG= "MVPTestActivity" private";
 
EditText ed_username private;
EditText ed_password private;
Button btn_login private;
LoginPresenter mLoginPresenter private;
@Override
Void onCreate protected (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
Void onClick public (V View) {
MLoginPresenter.loginSystem ();
 
}
});
}
/ * *
* returns the user name information
* @return
* /
@Override
String getUserName public () {
Returned_username.getText ().ToString ().Trim () () ();
}
/ * *
* returns the user password information
* @return
* /
@Override
String getPassword public () {
Returned_password.getText ().ToString ().Trim () () ();
}
/ * *
* login success callback
* personBean @param
* /
@Override
Void showSuccessInfo public (PersonBeanpersonBean) {
Log.d (TAG, "showSuccessInfo:" +personBean.toString ());
ShowToastMsgShort ("login success:" +personBean.toString ());
}
/ * *
* login failed callback
* /
@Override
Void showFailedInfo public () {
Log.d (TAG, "showFailedInfo...");
ShowToastMsgShort ("login failed...");
}
}

          In the end it is less of an interactive bridgePresenter:

4.4.3.PreSenterlayer:AsModelandViewThe interaction between the bridge, in this case to perform the login operation, and then go to the Model business in the implementation of the login, and finally the results of the login information to return to the View layer, is so 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;
 
/ * *
* the current class comment: responsible for the completion of the login interface Model in View (IPersonBiz) between the interaction
* project name: FastDev4Android
* package name: com.chinaztt.fda.presenter
* Author: Jiang Qingqing 15/10/27 16:36 on
* mailbox: jiangqqlmj@163.com
* QQ: 781931404
Jiangsu Transit Technology Co., Ltd.
* /
Class LoginPresenter public {
Static final String TAG= "LoginPresenter" private";
ILoginView mLoginView private;
IPersonBiz mPersonBiz private;
 
Handler mHandler=new Handler private ();
 
LoginPresenter public (view ILoginView) {
MLoginView = view;
MPersonBiz = PersonBizImp new ();
}
 
Void loginSystem public () {
MPersonBiz.login (mLoginView.getUserName) (), mLoginView.getPassword (), newLoginRequestCallBack () {
/ * *
* login successfully
* personBean @param
* /
@Override
Void loginSuccess public (personBean finalPersonBean) {
Log.d (TAG), "login success:" + personBean.toString ());
MHandler.post (Runnable new) {
@Override
Void run public () {
MLoginView.showSuccessInfo (personBean);
}
});
}
/ * *
* login failed
* /
@Override
Void loginFailed public () {
Log.d (TAG, "login failed...");
MHandler.post (Runnable new) {
@Override
Void run public () {
MLoginView.showFailedInfo ();;
}
});
}
});
}
}

Here we are.MVPThe example of the model is generally completed, look at the above demonstration on the effect of the OK.

Our project has been configured to integrate MVPDemoExamples.Welcome everybodyGithubSite forCloneOr download the browser:Https://github.com/jiangqqlmj/FastDev4Android Welcome everybody at the same timeStarandForkThe whole open source rapid development framework project~

Respect the original, reproduced please indicate: Sky From Dian Qing (Http://prog3.com/sbdm/blog/developer_jiangqqRights reserved)!

Pay attention to my subscription number (codedev123), sharing mobile development technology (Android/IOS), project management and blog articles every day!


Focus on my micro blog, you can get more exciting content



top
Twelve
step on
One
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • visit493544 times
    • Integral:Seven thousand eight hundred and fifty-seven
    • Grade
    • Rank:1404th name
    • original205
    • Reproduced:5
    • Translation:0
    • Comments:458
    contact information


  • Name: Jiang Qingqing
  • Undergraduate: Beifang Univesity of Nationality
  • 2015 annual -CSDN blog star
  • Personal site:Www.lcode.org
  • Mailbox: jiangqqlmj@163.com
  • QQ:781931404
  • Android rookie news group: 99787482
  • Native React exchange group: 282693535
  • Android development group 3:109244103
    Blog column
    Latest comments