What is an MVP? What does it have to do with the MVC that we often hear about and the difference?
The MVP is evolved from the classic pattern MVC, where the basic ideas are interlinked: Controller/presenter is responsible for the processing of logic, model provides data, and view is responsible for the display. As a new model, there is a significant difference between MVP and MVC: In MVP, view does not use model directly, communication between them is through presenter (Controller in MVC), all interactions occur within presenter, In MVC, view reads data from the direct model rather than through the Controller. In MVC, view is directly accessible to the model! As a result, the view contains the model information and inevitably includes some business logic. In the MVC model, more attention is paid to the constant of the model, while there are several different displays of the model, and the view. So, in the MVC model, models do not depend on view, but view is dependent on model. Not only that, because some business logic is implemented in view, it is difficult to change the view, at least those business logic is not reusable.How does the MVP solve the MVC problem?
in In the MVP, presenter completely separates the model from the view, and the main program logic is implemented in presenter. Furthermore, presenter is not directly related to the specific view, but interacts through a well-defined interface, allowing the presenter to be kept constant when the view is changed, that is, reuse! Not only that, we can also write Test view, simulate the user's various operations, so as to achieve the test of presenter-without the need to use automated testing tools. We can even test the logic of presenter by writing a mock Object (that is, an interface that implements the model and view, but no concrete content) when the model and view are not finished. In the MVP, the logic of the application is mainly implemented in presenter, where the view is a very thin layer. As a result, a design pattern for presenter first was proposed, which was to design and develop presenter based on user story. In this process, the view is simple enough to display the information clearly. At the back, you can change the view as you want, without any effect on the presenter. If the UI to be implemented is complex, and the associated display logic is related to model, you can place a adapter between the view and the presenter. This adapter to access the model and view to avoid the association between the two. At the same time, because the adapter realizes the interface of the view, it can guarantee the same interface with the presenter. This guarantees the simplicity of the interface between the view and the presenter, without losing the flexibility of the UI. In MVP mode, view should only have a simple Set/get method, the user input and Settings interface display content, in addition to this should not have more content, never allow direct access to model--this is a big difference with MVC. Benefits of MVP:1, the model and the view completely separate, we can modify the view without affecting the model 2, can more efficient use of the model, because all the interaction occurs in a place--presenter internal 3, we can use a presenter for multiple views, without the need to change presenter logic. This feature is very useful because view changes are always more frequent than models. 4. If we put logic in presenter, then we can test the logic out of the user interface (unit test)
So what about the MVP thing? So how do you write an MVP project? Please see:look at the project directory structure first
we simulate a requirement: first we have to enter aSplash Interface,in the splash interface, there is a
ProgressBar control and TextView control, we determine whether it has a network connection, if any, hide ProgressBar and jump to mainactivity if there is no network, then show ProgressBar and TextView, TextView prompts the user for no internet. As a simple requirement, let's look at how to do this with the MVP model.
First, let's see how the M-layer is written.
<span style= "FONT-SIZE:18PX;" >package Com.manning.androidhacks.hack020.presenter.model;public interface Iconnectionstatus { Boolean IsOnline ();} </span>
then look at the implementation (we mainly look at the use of MVP mode, so we do not do the network connection check, simulate a state)
<span style= "FONT-SIZE:18PX;" >package Com.manning.androidhacks.hack020.presenter.model.impl;import Com.manning.androidhacks.hack020.presenter.model.iconnectionstatus;public class Connectionstatus Implements Iconnectionstatus { @Override public boolean isOnline () { ///Todo:here We should place the code to check the Connectivity. return true; }} </span>
and then we're going to look at M's interface
<span style= "FONT-SIZE:18PX;" >package Com.manning.androidhacks.hack020.view;public interface Isplashview { void showprogress (); void Hideprogress (); void Shownoineterrormsg (); void Movetomainview ();} </span>
and the realization of M
<span style= "FONT-SIZE:18PX;" >package Com.manning.androidhacks.hack020.view.impl;import Android.app.activity;import android.content.Intent; Import Android.os.bundle;import android.view.view;import Android.widget.progressbar;import Android.widget.TextView ; Import Com.manning.androidhacks.hack020.r;import Com.manning.androidhacks.hack020.presenter.SplashPresenter; Import Com.manning.androidhacks.hack020.view.isplashview;public class Splashactivity extends Activity implements Isplashview {private TextView mtextview; Private ProgressBar Mprogressbar; Private Splashpresenter Mpresenter = new Splashpresenter (); @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.splash); Mpresenter.setview (this); Mtextview = (TextView) Findviewbyid (R.id.splash_text); Mprogressbar = (ProgressBar) Findviewbyid (R.id.splash_progress_bar); } @Override protected void Onresume () {super.onresume (); Mpresenter.didfinIshloading (); } public void ShowProgress () {mprogressbar.setvisibility (view.visible); } public void Hideprogress () {mprogressbar.setvisibility (view.invisible); } public void Shownoineterrormsg () {mtextview.settext ("No Internet"); } @Override public void Movetomainview () {startactivity (new Intent (this, mainactivity.class)); }}</span>
Finally, let's see how the P-layer controls their logic:
<span style= "FONT-SIZE:18PX;" >package Com.manning.androidhacks.hack020.view.impl;import Android.app.activity;import android.content.Intent; Import Android.os.bundle;import android.view.view;import Android.widget.progressbar;import Android.widget.TextView ; Import Com.manning.androidhacks.hack020.r;import Com.manning.androidhacks.hack020.presenter.SplashPresenter; Import Com.manning.androidhacks.hack020.view.isplashview;public class Splashactivity extends Activity implements Isplashview {private TextView mtextview; Private ProgressBar Mprogressbar; Private Splashpresenter Mpresenter = new Splashpresenter (); @Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.splash); Mpresenter.setview (this); Mtextview = (TextView) Findviewbyid (R.id.splash_text); Mprogressbar = (ProgressBar) Findviewbyid (R.id.splash_progress_bar); } @Override protected void Onresume () {super.onresume (); Mpresenter.didfinIshloading (); } public void ShowProgress () {mprogressbar.setvisibility (view.visible); } public void Hideprogress () {mprogressbar.setvisibility (view.invisible); } public void Shownoineterrormsg () {mtextview.settext ("No Internet"); } @Override public void Movetomainview () {startactivity (new Intent (this, mainactivity.class)); }}</span>
Well, I personally understand that is to take the logic layer out into the P-layer, if the requirements of the logical changes can only need to modify the P-layer or the logic of the approximate we can directly from writing a p can also, Now I have seen most of the development of all the things are written in the activity in this way to meet changes in the need to change frequently, the activity will be written in a mess, so think of this MVP model hope to help everyone, do you think the MVP is very good? Feel good students are applied to your actual development in the field to go!
Ym--android developing MVP mode (solves the coupling of view and model)