<span id="Label3"></p><strong><strong>Preface</strong></strong><p><p>The use of MVP in Android has been developed for a long time, just as Google has a series of architecture samples, here to collate the recognition and practice of the mvp, This article will be updated with the rich experience of use.</p></p><strong><strong>1. Introduction to MVC</strong></strong><p><p>Before the use of MVP development, we have been using the MVC pattern, in fact, not the mvc, generally we hear that the activity in Android is both view and controller, that activity is responsible for both view display, Also responsible for dealing with business logic, which we generally hear, but in fact, our activity is responsible for the data preservation and reading.</p></p><p><p>When the business logic is simple, just get the data, show the data, our code is this:<br></p></p><p><p>But in fact, consider the storage of data, business logic, and so on, we are:<br><br>Everything is connected, so complex that it becomes a face, the face of this god Object:activity.</p></p><p><p>Think about it, do we write everything in this god class: Web requests, parsing json, processing data, displaying data, caching data, and so on, all in the activity, even though some things have been packaged well, but still are involved with the activity, Basically all the code is in the activity, so called mvc, is called <code>massive view controller</code> , Super Big Viewcontroller.<br>In spite of the advantages of doing so, it is easy to see the business logic, and it is very convenient and handy to write it unconsciously. But obviously, as the demand continues to increase, the code will write more, and if the requirements are changed, it is difficult to change, and this is not easy to refactor, because it is too complex to move a bit may be a Bug.</p></p><p><p>So what if you don't want to do this? Then there is the MVP.</p></p><strong><strong>2. Introduction of MVP</strong></strong><p><p>The typical MVP is like This:<br><br><br></p></p><p><p>It is clear that:</p></p> <ol> <ol> <li>In mvp, presenter and view interact with each other.</li> <li>MVP rejects view and model direct interaction, and view and model can only interact indirectly through Presenter. This is reasonable, because Android must operate on Uithread on the ui, and the model is mostly Asynchronous. You can easily handle threading issues by transferring to a layer.</li> <li>Model and view are passive, and everything is dominated by presenter.</li> </ol> </ol><p><p>We generally think of activity/fragment as a view, then our activity/fragment code is much simpler, the response presenter instructions to view operation, and these operations are defined in the <code>IVIEW</code> interface. So most of the code is transferred to the presenter, and some of the business code is encapsulated in the model layer, so the code will be much clearer, after writing a look, wow, too clear. and it's easy to locate bugs, after all, the clearer the code responsibilities, the less Error-prone.</p></p><p><p>So now separate to introduce:</p></p> <ul> <ul> <li>Model<br>The data provided for the UI layer, or the data passed down from the UI Layer.<br>The Model is an abstraction that the user interface needs to display data, or it can be understood as an abstraction from the business data (results) to the user interface.<br>Model should encapsulate business logic and not let presenter and view know the business logic layer</li> <li>View<br><em>View is a layer this displays data and reacts to user Actions.</em><br>View provides friendly interaction, displays data, responds to user actions, and is forwarded to presenter for specific processing</li> <li>Presenter<br>Logical control layer, Distribution Logic. Handles the distribution of various logic of the program, receives feedback commands on the View layer ui, timed commands, system commands, and so on, and distributes the processing logic to the business layer to do specific business operations, and then displays the resulting Model to the VIEW.</li> </ul> </ul><p><p>here, about the basic concept of mvp, is divided into three layers to write code, here is recommended to use the MVP when <strong>subcontracting according to module Sub</strong> -package, module comparison, build a module package, all modules, each module subdivided three layers, so the package is clear, the code is clear.</p></p><p><p>Next look at some examples of google.</p></p><strong><strong>3. The MVP in Google samples</strong></strong><p><p>The MVP in Google Samples (this image in the Google MVP example really perfectly illustrates the architecture of a simple app):<br></p></p><p><p>When the model layer gets the data, it uses <code>DataRepository</code> distribution management, receives instructions from the presenter to <code>RemoteDataSource</code> fetch the data from or <code>LocalDataSource</code> , and callbacks the Presenter,presenter back to the view for Display.</p></p><p><p>Here are some of the <strong>official recommended</strong> points that can be found in the Code:</p></p> <ul> <li><li><p>use <em>Fragments as View. The separation between activity and Fragment fits nicely with this implementation of mvp:the activity are the overall cont Roller that creates and connects views and Presenters,which means the Activity are responsible for the creation of fragment S and Presenters.</em><br>Use fragment as the view,activity as the global controller to bind presenter and view Together.</p></li></li> <li><li><p><em>Create a <code>contract</code> interface defining the View and the Presenter,which contains the view interface and the presenter Inter Face. Contracts is interfaces used to define the connection between views and Presenters.</em> This is <strong>a amazing concept.</strong><br>Create a <code>IContract</code> parent class interface to manage and interface under the same <code>IView</code> module <code>IPresenter</code> , so it is clear that the logic of both can be clearly seen.</p></li></li> <li><li><p><em>in general, the business logic lives in the presenter and relies on the "view to do the" Android UI Work.</em><br>The business logic is all in the presenter layer, depending on the view layer to Trigger.</p></li></li> <li><li><p><em>The view contains almost no logic:it converts the presenter ' s commands to UI actions and listens to user actions, WHI CH is passed to the Presenter.</em><br>View has virtually no logic to respond to presenter layer instructions and receive user actions passed to Prenster. of course, very few very light logic should be dealt with in this layer (empty processing, etc.), which should be measured by itself. <strong>The view should be Passive. </strong></p></li></li> </ul><strong><strong>4. MVP is a methodology</strong></strong><p><p>We should easily see that there are only a few basic principles in the mvp, there is no fixed way to achieve the mvp, for the business needs of their own, logic to better use the mvp, as long as the view and model separation, code clear, easy to debug, easy to test, all Ok.</p></p><p><p>Some of the guiding principles of MVP are to constrain the implementation of:</p></p> <ul> <ul> <li>Model and view cannot communicate directly, only through presenter</li> <li>Presenter a role similar to a middleman in coordination and scheduling</li> <li>Model and view are interfaces, presenter holds a model interface and a view interface</li> <li>Both model and view should be passive, and everything is dominated by presenter.</li> <li>Model should encapsulate the interaction with the business logic layer, in other words presenter and view should not know the business logic layer</li> <li>The logic of the view should be as simple as possible and should not have a State. When an event occurs, call presenter to process it, and do not pass arguments, and then call the view method to get the presenter Processing.</li> </ul> </ul><p><p>Knowing that MVP is a methodology, here's another way to achieve the MVP.</p></p><strong><strong>5. Another way to achieve MVP</strong></strong><p><p>The other way to implement the MVP (variant) is not to use activity/fragment as a view, but instead to take them as presenter to manage the view in a single class.</p></p><p><p>Using this method can greatly reduce the number of classes, because it is to rely on <strong>generics</strong> to decouple, so you can not provide interfaces, of course, I have to remove the interface in the implementation process, while the response of the Click event as a logic, put in the presenter, so it is not transmitted to The. (before using fragment as the view, click ListItem need to modify the read state, passed into the presenter changes, presenter after the modification and call the View.showdetailui () method and return to the view, This will greatly reduce the workload after using the Variant)</p></p><p><p>Use this way personally think a bit strange, one is the use of generics to increase the understanding of the difficulty, write write do not know how to call to this piece, the second is to remove the interface after the sense of reusability, logic can not be reused, view cannot be reused, of course, do not delete the interface and the original MVP what is the difference? yes, but this may be as the more you use the mvp, the more you will understand, whether the activity is a view or a controller, may be different depending on the project, it will be suitable for different situations.</p></p><p><p>So after the MVP variant, Here's a summary of the personal use of mvp.</p></p><strong><strong>6. MVP Experience</strong></strong><p><p>Using the MVP development, suddenly feel a lot more refreshing, although a lot of code, but his flaws, let the code refreshing a Lot. It was really great to see the refactoring code and feel like I was living in a nightmare before I finally had to write all the code in the activity or Fragment. The following is a concrete talk about the experience of the Use.</p></p><p><p>The first is Methodological:</p></p> <ul> <ul> <li><p>first, clear what is the distribution of business logic, business logic, and view Logic.<br>Understanding these three points is a great help in designing interfaces, using MVP to write projects, and Refactoring. View logic can better write view, distribute business logic for presenter, business logic for model, and of course some business logic is in Presenter.</p></li> <li><p>Then look at what the view is, what the logic is, what the logic is showing which view, which operations invoke which logic, which helps us to sort out the code logic and make it easy to call each Other.</p></li> <li><p>A view can have more than one Presenter, to use what business to add what Presenter, and to implement this Presenter the required View interface, which is simple multiplexing logic, that is, Presenter is Reusable.</p></li> </ul> </ul><p><p>Then it's on the Code:</p></p> <ul> <ul> <li><code>IView</code>The methods in the interface are all ui-related, show (), hide (), etc., should not be logically related.</li> <li><code>IPresenter</code>The methods in the interface should be all logically related and distributed, and it is clear that the model reference should be held to facilitate manipulation of the Data. There should be no model-related methods, such as loadfromlocal,loadfromremote, which should be handled by datarepository. Presenter just need to tell model that I need this data, that is, loaddata, not the load data from the Tube. This only needs to be responsible for <strong>distribution!!! </strong></li> <li><code>Model</code>, the data is processed, all the data goes in and out, and the data is Processed. Generally create a <code>DataRepository</code> class pair <code>LocalDataSource</code> , <code>RemoteDataSource</code> for management distribution, These three classes all implement <code>IDataSource</code> interfaces, ensure logical Consistency.</li> </ul> </ul><p><p>Finally some personal experience:</p></p> <ul> <ul> <li><p>Within the mvp, there is a growing sense of the importance of dependency Injection. All are dependent, all need to inject, feel the dependency injection frame <code>Dagger2</code> is the time to show its powerful Function.</p></li> <li><p>Too many callbacks. When getting the data, starting from the presenter callback, then to datarepository, then to the specific localdatasource or Remotedatasource callback, a total of three layers of callback, the growth of the Chain. obviously, to solve this problem need to use <code>RxJava</code> , can save a lot of code, become concise is Inevitable.</p></li> <li><p>The interface management classes in Google samples are <code>IContract</code> really great, managing Ipresenter and iview, and if you can, let it also manage <code>IDataSource</code> this interface in a very clear way.</p></li> <li><p>Although the MVP achieved a certain separation, but the equivalent of a large number of code from activity to presenter and model, although the use of interfaces to make the code much clearer, but not ugly and difficult to find, as the business continues to complicate, the code will continue to be more and more. So using the clean architecture should be the Best.</p></li> <li><p>finally, we have to say that the number of methods, classes have increased a lot. But It's also inevitable. decoupling, Convenient Testing.</p></li> </ul> </ul><strong><strong>7. Code Implementation</strong></strong><p><p>Said so, show me the Code.</p></p><p><p>so, the code that you write is all on github, all in a branching form:</p></p> <ul> <ul> <li><p>The most basic branch, all code in Activity: Branch_basic</p></li> <li><p>MVP division, using MVP refactoring Code: BRANCH_MVP</p></li> <li><p>MVP another way to achieve (variant) branch: branch_mvp_variant</p></li> </ul> </ul><p><p>After that, you will definitely try to refactor the clean schema Again. But because the project itself is small, so it is really good to be a practiced hand.</p></p><p><p><br></p></p> <blockquote> <blockquote> <p>Reference:</p> <ul> <li><p>Introduction of mvp, very good article<br>An easy-to-understand way to introduce the ANDROID MVP model<br>MVP in Android<br>Introduction to Model View Presenter on Android<br></p></li> <li><p>MVP Variant:<br>Develop Android apps with MVP architecture<br>Android MVP?—? An Alternate approach</p></li> <li><p>MVP methodology, a philosophical article, after reading it will feel that the original code can be so philosophical:<br>Talk about the MVP mode of Android</p></li> </ul> </blockquote> </blockquote> <p><p>Android Project Framework uses MVP development</p></p></span>
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