Ym -- MVP mode for Android Development (solving the coupling between View and Model), androidmvp

Source: Internet
Author: User

Ym -- MVP mode for Android Development (solving the coupling between View and Model), androidmvp

What is MVP? What is the relationship and difference between it and the MVC we often hear?

MVPs evolved from the classic MVC Model. Their basic ideas are the same: Controller/Presenter is responsible for logical processing, Model provides data, and View is responsible for display. As a new Model, MVPs and MVC have a major difference: in MVPs, views do not directly use models, and the communication between them is through Presenter (Controller in MVC) all interactions occur within the Presenter. In MVC, the View reads data from the direct Model rather than through the Controller. In MVC, View can directly access the Model! As a result, the View will contain Model information, and some business logic will inevitably be included. In the MVC Model, the Model is not changed, but there are multiple different models and views. Therefore, in the MVC Model, the Model does not depend on the View, but the View depends on the Model. Not only that, because some business logic is implemented in the View, it is difficult to change the View. At least those business logic cannot be reused. How can MVPs solve MVC problems?
In MVP, Presenter completely separates Model and View, and implements the main program logic in Presenter. In addition, the Presenter is not directly associated with a specific View, but interacts with the specific View through a defined interface, so that the Presenter can be retained when the View is changed! In addition, we can also compile the View for testing to simulate various user operations, so as to test the Presenter-without using automated testing tools. When neither Model nor View is complete, we can write Mock Object (that is, the Model and View interfaces are implemented, but there is no specific content) to test the Presenter logic. In MVP, the logic of the application is mainly implemented by the Presenter, and the View is a thin layer. Therefore, some people have proposed the Presenter First design mode, which is to First design and develop Presenter based on the User Story. In this process, the View is very simple, and the information can be clearly displayed. Later, you can change the View as needed without affecting the Presenter. If the UI to be implemented is complex and the relevant display logic is related to the Model, you can place an Adapter between the View and Presenter. This Adapter is used to access the Model and View to avoid the association between the two. At the same time, because the Adapter implements the View interface, it can ensure that the interface with the Presenter remains unchanged. This ensures that the interface between View and Presenter is concise without losing the UI flexibility. In MVP mode, the View should only have a simple Set/Get method. The user inputs and sets the content displayed on the interface, so there should be no more content, direct access to the Model is never allowed-this is a big difference with MVC. Advantages of MVP: 1. The model and view are completely separated. You can modify the view without affecting the model. 2. You can use the model more efficiently, because all interactions occur in one place-inside the Presenter 3. We can use a Presenter for multiple views without changing the logic of the Presenter. This feature is very useful because views change more frequently than models. 4. If we put the logic in the Presenter, we can test the logic (unit test) from the user interface)
So how can I write an MVP project with so many questions about MVP? See: first, from the project directory structure above.
First, we need to enter a Splash interface.

The ProgressBar control and TextView control determine whether there is a network connection. If there is a network connection, the ProgressBar and the jump to MainActivity will be hidden. If there is No network, the ProgressBar and TextView will be displayed, and the TextView will prompt the user No internet. Let's look at how to use the MVP model to meet this simple requirement.

First, let's take a look at how the M-layer interface is written.

package com.manning.androidhacks.hack020.presenter.model;public interface IConnectionStatus {  boolean isOnline();}
Then let's look at the implementation (we mainly look at the use of MVP mode, so we will not check the network connection here, and simulate a status)

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;  }}
Then let's look at M's interface.

package com.manning.androidhacks.hack020.view;public interface ISplashView {  void showProgress();  void hideProgress();  void showNoInetErrorMsg();  void moveToMainView();}
And M implementation

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));  }}

Finally, let's take a look at how the P layer controls their logic:

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));  }}

Well, I personally understand that it is to extract the logic layer into the P layer, if a logical change is required, you only need to modify the P layer, or you can write a P directly from the logic, most of the development programs I have seen have written everything in the Activity so that when the demand for frequent changes is met, the Activity will be written in disorder, so I thought of this MVP model and hoped to help everyone. Do you think MVP is very good? If you think it is good, you can apply it to your actual development ~!






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.