The application of MVP mode in Android development

Source: Internet
Author: User
<span id="Label3"></p><span style="font-size:24px"><span style="font-size:24px"><strong><span style="color:#009900">I. Introduction of the MVP</span></strong></span></span>As UI creation technology becomes more and more powerful, the UI layer is performing more and more Responsibilities. To better subdivide the functionality of the view and model, The view focuses on visualizing the data and interacting with the User. At the same time let the model only deal with relational Data.     The MVP (model-view-presenter) model based on MVC concept Emerged. There are usually 4 elements in MVP Mode: (1)<strong><strong><span style="color:#006600">View</span></strong></strong>: Responsible for drawing UI elements, interacting with the user (embodied as activity in android); (2<strong><strong> <span style="color:#009900">)</span><span style="color:#006600">View interface</span> </strong></strong>: Requires view implementation interface, view through the View interface and presenter interaction, reduce coupling, convenient for unit test; (3)<span style="color:#006600"><span style="color:#006600"><strong>Model</strong></span></span>: Responsible for storing, retrieving, manipulating data (and sometimes implementing a model interface to reduce coupling); (4)<span style="color:#006600"><span style="color:#006600"><strong>Presenter</strong></span></span>: As an intermediate link between view and model, handles the responsible logic for interacting with the User.<p><p></p></p><p><p><br><br>(original address: Http://blog.csdn.net/vector_yi/article/details/24719873)<br><span style="font-size:24px"><strong><span style="color:#009900">second, Why use MVP mode</span></strong></span> in Android Development. Activity is not a controller in the standard MVC Pattern. <span style="font-family:Arial; background-color:rgb(255,255,255); line-height:26px; text-align:left">its primary responsibility is to load the Application's layout and initialize the user interface. and accept and process the action request from the user, and then Respond. As the complexity of the interface and its logic Increases. The duties of the activity class are constantly being added, so that it becomes bloated. When we move the complex logic processing to another class (presneter), the activity is in fact the view in MVP mode, which is responsible for initializing the UI Elements. Establishes the association of UI elements with presenter (listener, and so on). At the same time, I will deal with some simple logic (complex logic to presenter Processing).</span> <span style="font-family:Arial"><span style="line-height:26px">also, What do you think about the unit test of code logic when you develop an Android app? Do you want to deploy the app to an Android emulator or a real machine every time, and then test it by simulating user actions? However because of the features of the Android Platform. A lot of time is spent on each deployment. This leads directly to a reduction in development efficiency. </span></span></p></p><p><p> In MVP mode, the presenter that handles complex logic interacts with the view (Activity) through interface, what does that mean? It shows that we can use our own definition class to implement this interface to simulate activity behavior to presenter unit test, save a lot of deployment and testing Time. <span style="font-family:arial"><span style="line-height:26px"> <br> </span> </span> <span style="font-size:24px"> <strong> Span style= "color: #009900" > iii. Similarities and differences between MVP and MVC </strong></span><strong> </strong>       Both the MVC pattern and the MVP model have been applied for many years as a development model for separating the UI layer from the business Layer. When we choose a development model, we first need to understand the pros and cons of such a pattern:      There is inevitably a drawback in either MVC or MVP mode:          <span style="font-size:24px"> <strong> <span style="color: #009900"> Extra code complexity and learning Costs. </span> </strong> </span>       This leads to the two development patterns that may not be very small applications.       But compared to their strengths, this drawback is basically negligible:      (1) reduced coupling       (2) module Responsibility division clearly       (3) facilitates test drive development       (4) code reuse       (5) hide data       (6) code flexibility <br>     & nbsp, There is also a very big difference between the two modes of MVP and MVC. </p></p><p><p>there <em>are some programs that apes choose not to use regardless of the pattern, part of the reason may be that they cannot differentiate between the two patterns</em> . The following are the most critical differences between the two modes: (reference article: http://www.infragistics.com/community/blogs/todd_snyder/archive/2007/10/17/ Mvc-or-mvp-pattern-whats-the-difference.aspx) MVP Mode:</p></p> <ul> <ul> <li><span style="font-size:24px"><strong><span style="color:#009900">view does not interact directly with the model</span></strong></span> . instead, They interact with the model indirectly by interacting with the Presenter.</li> <li>The interaction between the presenter and the view is done through the interface, which is more advantageous for adding unit test</li> <li>The view is usually one-to-presenter, but complex view may bind multiple presenter to handle the logic</li> </ul> </ul>MVC pattern: <ul> <ul> <li>View can interact directly with the model</li> <li>The controller is Behavior-based. And can be shared by multiple view</li> <li>Be responsible for deciding which view to display</li> </ul> </ul><br><span style="font-size:24px"><span style="font-size:24px"><strong><span style="color:#009900">Iv. Examples of Android development using MVP</span></strong></span></span>Said so many Theories.     Now it's the turn to Practice. Now let's implement an Android demo (): the ability to read and access user information from edittext, and to read and display user information from the background, based on the ID.<br><br>The page layout is very easy, not introduced. The following are coded according to the MVP Principle: first look at the folder structure of the Java file:<br>Can Find. Presenter and model, view are interactive through the interface, both to reduce coupling and convenient unit Test.<br><br>(1) First we need a Userbean. Used to save user information<pre code_snippet_id="318872" snippet_file_name="blog_20140429_1_4507328" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_1_4507328" name="code" class="java"> public class UserBean { private String mfirstname; Private String mlastname; Public UserBean (string firstName, string LastName) {this . mfirstname = firstName; This. mlastname = lastName; } Public String getfirstname () { return mfirstname; } Public String getlastname () { return mlastname; }}</pre></pre>(2) look at the view interface: according to the Requirements. View can read the three edittext of ids, FirstName, lastname, and write to FirstName and Lastname. This defines the Iuserview interface:<pre code_snippet_id="318872" snippet_file_name="blog_20140429_2_2806652" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_2_2806652" name="code" class="java">public interface Iuserview { int GetID (); String getfristname (); String getlastname (); void Setfirstname (String firstName); void Setlastname (String lastName);}</pre></pre>(3) Model Interface: same, The model also needs to read and write the three fields, and stored in a vector (this is not our concern, can exist in memory, file, database or remote Server. But for presenter and view no impact), define the Iusermodel interface:<pre code_snippet_id="318872" snippet_file_name="blog_20140429_3_4344578" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_3_4344578" name="code" class="java">public interface Iusermodel { void SetID (int id); void Setfirstname (String firstName); void Setlastname (String lastName); int GetID (); UserBean load (int id);//read user information by id, return a UserBean}</pre></pre><span style="font-size:13px"><span style="font-size:13px">(4) Presenter:</span></span><span style="font-size:13px">At this point <span style="font-size:13px">. Presenter is able to interact with the view and model via the Interface:</span></span><pre code_snippet_id="318872" snippet_file_name="blog_20140429_4_5882505" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_4_5882505" name="code" class="java">public class Userpresenter { private Iuserview muserview; Private Iusermodel musermodel; Public userpresenter (iuserview view) { muserview = view; Musermodel = new Usermodel (); } public void Saveuser (int id, string firstName, string LastName) { musermodel. SetID (id); Musermodel. setfirstname (firstName); Musermodel. setlastname (lastName); } public void Loaduser (int id) { UserBean user = musermodel. load (id); Muserrview. setfirstname (user. getfirstname ());//update the display muserview. setlastname by calling the Iuserview method . User. Getlastname ());} }</pre></pre><span style="font-size:13px"><span style="font-size:13px"><span style="font-family:Consolas">(5) useractivity:</span></span></span><span style="font-size:13px"><span style="font-size:13px"><span style="font-family:Consolas">Useractivity implements the Iuserview and View.onclicklistener interfaces, with a userpresenter member variable at the same time:</span></span></span><pre code_snippet_id="318872" snippet_file_name="blog_20140429_5_9928686" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_5_9928686" name="code" class="java">public class Useractivity extends Activity implements onclicklistener, Iuserview { Private EditText mfirstnameedittext, mlastnameedittext, midedittext; Private Button msavebutton, mloadbutton; Private Userpresenter muserpresenter;</pre></pre><br><span style=""><span style="">overridden the onclick method:</span></span><pre code_snippet_id="318872" snippet_file_name="blog_20140429_6_2023021" name="code" class="java"><pre code_snippet_id="318872" snippet_file_name="blog_20140429_6_2023021" name="code" class="java">@Override public void OnClick (View v) { //TODO auto-generated method stub Switch (v. getId ()) {case R. Id. savebutton: muserpresenter. saveuser (getID (), getfristname (), getlastname ()); break; Case R. ID. loadbutton: muserpresenter. loaduser (getID ()); break; Default: Break ; } }</pre></pre><br><span style="font-family:Consolas"><span style="font-family:Consolas"><span style="font-size:11pt">you can see that view is only responsible for interacting with the User. And the data-related logic operations are thrown to the presenter to do. When presenter calls model to finish processing the data, the information displayed by the view is updated by IUSERVIEW. </span></span></span><span style="font-family:Consolas"><span style="font-family:Consolas"><span style="font-size:11pt"><br></span></span></span><span style="font-family:Consolas"><span style="font-family:consolas"><span style="font-size:11pt">view The remaining methods and Usermodel classes are not the focus of our Concern. Suppose you are interested in the source code on Github: https://github.com/VectorYi/MVPSample.git or click on the link below to download it. <p> </p> <p> Source code in this <br> <br> <br> <br> <br> <br> </p> <p> The application of MVP mode in Android development </p> </span> </span></span></span>

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.