Android design mode-MVP mode

Source: Internet
Author: User
Tags gettext

First, what is the MVP mode

MVP (Model/view/ Presenter) evolved from classic pattern MVC,Presenter instead of activity and fragment became controllers, and activity and fragment focus on what the view layer should do.



second, the advantages of MVP1, the model and the view separated, the level is clearer.


2, presenter can be reused.


3, if we put logic in the presenter, the unit test is more simple


Third, actual combatbelow is the chart of my project.



MVP in the Parsenter after processing logic through the interface to notify the view layer, I put these interfaces are in the aisle package. Here's a look at the code in the interface and data Model

Public interface ICard {    void onstartloading ();//start loading    void onloading ();//load in    void Onloadingdone (Card );//Load succeeded}

public class Card {public    card (string name, String tel) {        this.name = name;        This.tel = Tel;    }    public String name;    Public String Tel;    Public String GetName () {        return name;    }    Public String Gettel () {        return tel;    }    Public String toString () {        return '  name: ' + name + '  Tel: ' + tel;    }}




Very common interface and data model, the interface is used to notify the view refresh UI. The following is the code in Parsenter
public class Cardparsenter {    private String TAG = "Cardparsenter";    Private ICard ICard;    Public Cardparsenter (ICard ICard) {        this.icard = ICard;    }    public void Getcard () {        icard.onloadingdone (new Card ("Xiaoming", "12345678"));    public void Setcard (card) {        log.i (TAG, card.tostring ());}    }

Parsenter holds a view layer interface for callback notifications, and the following is the view layer code
public class Useractivity extends appcompatactivity implements ICard {public cardparsenter cardparsenter;    Public EditText Nameet, Telet;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Toolbar Toolbar = (Toolbar) Findviewbyid (R.id.toolbar);        Setsupportactionbar (toolbar);        Cardparsenter = new Cardparsenter (this);        Nameet = (EditText) Findviewbyid (R.id.nameet);    Telet = (EditText) Findviewbyid (R.id.telet); ' public void click ' (view view) {switch (View.getid ()) {case R.id.setbt:cardparsente                R.setcard (New Card (Nameet.gettext (). ToString (), Telet.gettext (). ToString ()));            Break                Case R.id.getbt:cardparsenter.getcard ();        Break   }} @Override public void Onstartloading () {} @Override public void Onloading () {} @Override public void Onloadingdone (card) {Nameet.settext (Card.getname ());    Telet.settext (Card.gettel ()); }}

View implementation Icard interface, and passed to Parsenter, the channel is established. And finally the layout code.
<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "xmlns:app=" Http://schemas.android.com/apk/res-auto "xmlns:tools=" Http://schemas.android.com/tools "Andro Id:layout_width= "Match_parent" android:layout_height= "match_parent" android:paddingbottom= "@dimen/activity_ Vertical_margin "android:paddingleft=" @dimen/activity_horizontal_margin "android:paddingright=" @dimen/activity_ Horizontal_margin "android:paddingtop=" @dimen/activity_vertical_margin "app:layout_behavior=" @string/appbar_ Scrolling_view_behavior "tools:context=". View. Useractivity "tools:showin=" @layout/activity_main "> <edittext android:id=" @+id/nameet "android:l Ayout_width= "250DP" android:layout_height= "wrap_content" android:hint= "Nameet" android:textsize= "20sp "/> <edittext android:id=" @+id/telet "android:layout_width=" 250DP "android:layout_height=" W Rap_coNtent "android:layout_below=" @+id/nameet "android:hint=" tel "android:textsize=" 20sp "/> <but        ton android:id= "@+id/setbt" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_below= "@+id/telet" android:onclick= "click" android:text= "Set" android:textsize= "20sp"/> <button android:id= "@+id/getbt" android:layout_width= "Wrap_content" android:layout _height= "Wrap_content" android:layout_below= "@+id/telet" android:layout_toendof= "@+id/setbt" Android: onclick= "click" android:text= "Get" android:textsize= "20SP"/></relativelayout>
Iv. Summary

One drawback of MVC is that the view layer and the model layer are coupled together, causing the activity to be controlled as well as the view layer (such as setting up listening, animating, initializing controls ...). The MVP separates the view layer and the model layer on the basis of MVC, then controls the model with Parsenter, and notifies the view to refresh the UI when the results are obtained, thus avoiding coupling.



Code Address: http://download.csdn.net/detail/s4336723/9515111

Framework of an MVP model: Https://github.com/sockeqwe/mosby



Android design mode-MVP mode

Related Article

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.