is MVP mode suitable for us to use? Code talk

Source: Internet
Author: User

Reprint please indicate the source: Wang 亟亟 's way of Daniel

According to the Convention today on the Immersion menu bar of the article is not written, and then before work to see some interesting content on the next, MVP mode before there is a general understanding but no actual combat in the android used. (There are some Chinese Daniel have some similar content, but everyone's understanding of one thing may be roughly the same but there is always a slight difference, mainly to my understanding and realization of this thing to share to everyone)

I'm not too fond of moving too many people have been finishing things and then post out to make their own theory of multi-niu B, I still use code and examples to speak, then here to leave some of the concepts that others have talked about the concept of things to build concepts, these design levels of things or want you to see the code after reading, Otherwise, the first reaction is to look at the code directly.

Xiang elder brother: http://blog.csdn.net/lmj623565791/article/details/46596109
rocko:http://rocko.xyz/2015/02/06/android%e4%b8%ad%e7%9a%84mvp/

OK, first up and down

In case of reasonable operation

In the case of losing something less

Obsessive compulsive disorder, I try to make sure that my example itself is content, at least not just drag 2 "dirt" of the dead control to use, so write now (5 o'clock off duty now 8 o'clock more. The belly is very hungry. )

First up and down we use the library (all I have said before, thanks again for the open-source thighs!!) )

dependencies {    compile fileTree(dir‘libs‘, include: [‘*.jar‘])    ‘junit:junit:4.12‘    ‘com.android.support:appcompat-v7:23.2.1‘    ‘com.android.support:design:23.2.1‘    ‘com.rengwuxian.materialedittext:library:2.1.4‘    ‘com.jaeger.statusbaruitl:library:1.0.0‘    ‘com.jakewharton:butterknife:7.0.1‘    ‘com.apkfuns.logutils:library:1.2.2‘}

And look at the package catalogue.

In fact, only 2 activity one login to a main page, the login directory is all for landing (at this time, the following uproar, I x how a login so many classes/interfaces)

Consider here for a long time or feel that the source code directly posted here is more appropriate, convenient reference understanding (the name did not take good, my pot)

Activity that is responsible for login

publicvoidUserNameError()  //处理用户名错误publicvoidPassWordError()  //处理密码错误publicvoidLoginSuccess()  //登陆成功跳转
 Public  class loginactivity extends appcompatactivity  implements  Loginlogic {    @Bind(R.id.fab) Floatingactionbutton fab;@Bind(R.id.password) Materialedittext PassWord;@Bind(r.id.username) Materialedittext userName; Clickinterface Clickinterface;@Override    protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate);        Setcontentview (R.layout.activity_login); Butterknife.bind ( This); Statusbarutil.settransparent ( This); Clickinterface =NewLogincontrol ( This); }@OnClick(R.id.fab) Public void Login(View view) {LOGUTILS.D ("--->fab Login");    Clickinterface.login (Username.gettext (). ToString (). Trim (), Password.gettext (). ToString (). Trim ()); }@Override     Public void Usernameerror() {Username.seterror ("User name is incorrect"); }@Override     Public void Passworderror() {Password.seterror ("Wrong password"); }@Override     Public void loginsuccess() {mainactivity.logicsuccess (loginactivity. This); }}

User button trigger, 2 strings of incoming account and password

publicinterface ClickInterface {    void Login(String userName, String passWord);}

The interface that the previous attempt layer has tuned

publicinterface LoginLogic {    void UserNameError();    void PassWordError();    void LoginSuccess();}

Implements the interface used by the class to return the result Onloginfabclick the method of the specific execution of the login operation Dologin ()

publicinterface LogicInterface {    interface OnLoginFabClick {        void OnUserNameError();        void OnPasswordError();        void OnLoginSuccess();    }    void doLogin(String userNameStr, String passwordStr, OnLoginFabClick onLoginFabClick);}

The "Central processor" is a transit point for 2 processes, which serves as a staging point for both the attempt layer and the operating layer

 Public  class Logincontrol implements Logininterfaceimp. Onloginfabclick, clickinterface {Loginlogic loginlogic; Logicinterface Logicinterface; Public Logincontrol(Loginlogic loginlogic) { This. loginlogic = Loginlogic; This. Logicinterface =NewLogininterfaceimp (); }@Override     Public void Onusernameerror() {loginlogic.usernameerror (); }@Override     Public void Onpassworderror() {loginlogic.passworderror (); }@Override     Public void onloginsuccess() {loginlogic.loginsuccess (); }@Override     Public void Login(String userName, String PassWord) {Logicinterface.dologin (UserName, PassWord, This); }}

Specific to the implementation of the Landing behavior class, click on the final destination of the event and the starting point of the event feedback

/** * Created by Jiajiewang on 16/3/29. * * Public  class logininterfaceimp implements logicinterface {    @Override     Public void Dologin(String usernamestr, String passwordstr, Onloginfabclick Onloginfabclick) {BooleanFlag =true;if(Usernamestr.length () <1|| Usernamestr = =NULL) {onloginfabclick.onusernameerror (); Flag =false; }if(Passwordstr.length () <1|| Passwordstr = =NULL) {onloginfabclick.onpassworderror (); Flag =false; }if(flag)        {onloginfabclick.onloginsuccess (); }    }}

What the? Put a lot of not specific items can not be ordered to see the concept of unclear?

In order to prevent people have similar questions I have drawn to you, important things with large characters!!

Total Flow:

Is the overall process not very abstract and not very clear about the specific behavior? All right, I've got 2 lines of work.

This line is called " command to do the landing process line ."

This is called " tell an attempt to log in to the process line "

After reading these 3 pictures, do you understand what has been done?
You: "Don't understand"

So I'm going to tell you why it's designed to do this?

1. Clear hierarchy, regardless of how the other party to achieve, only focus on the results;
2. Using the MVP architecture at the View layer (Presentation layer) makes the original bloated activity (or fragment) simple, and its handling is given to presenter.
3. Easy to test, as long as the unit test based on each module alone to ensure the overall stability.
4. Easy to quickly iterate, based on low-coupling code, simply add the interface to the business logic and then implement it separately at the appropriate level, without compromising other functionality.

What the? So that's official? Let me give you one more example, we now have only one button and then he sent it out from the Clickinterface line, so I have a lot of buttons, lots and lots of behavior? Just add the required method to the Clickinterface and do not change the class entirely without the whole project.

Although the construction process is a bit disgusting + difficult to build, but so long as the end of the matter on the follow-up!

Here are some of the schematic diagrams of the next-door buckle:

Source Address: Https://github.com/ddwhan0123/BlogSample/blob/master/MVPDemo.zip

It's off,!!!!!!.

Of course, if you still do not understand can direct my real (not Xu malicious harassment)

Oh, Roar, accept Java Web PHP, looking for a job can also consider the next Oh!

is MVP mode suitable for us to use? Code talk

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.