Analysis on MVP mode in Android development and androidmvp Mode

Source: Internet
Author: User

Analysis on MVP mode in Android development and androidmvp Mode

Currently, there is no standard for MVP usage. Here we will record some of the MVP usage knowledge in Android.

Developed in the traditional way, the activities are often mixed with UI interaction, business logic, and other processes. The MVP model can skillfully solve this problem. Let's look at a small example.

/*** Defines an interface for operations on the UI component so that the Activity can implement this interface * @ author Quinn * @ date 2015-5-9 */public interface LoginView {public void showProgress (); public void hideProgress (); public void setUsernameError (); public void setPasswordError (); public void navigateToHome ();}

/*** The Activity needs to implement the View Interface and hold a Presenter object, convert all logic processing to Presenter * @ author Quinn * @ date 2015-5-9 */public class LoginActivity extends Activity implements LoginView, View. onClickListener {private ProgressBar progressBar; private EditText username; private EditText password; private LoginPresenter presenter; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_login); progressBar = (ProgressBar) findViewById (R. id. progress); username = (EditText) findViewById (R. id. username); password = (EditText) findViewById (R. id. password); findViewById (R. id. button ). setOnClickListener (this); presenter = new LoginPresenterImpl (this) ;}@ Overridepublic void showProgress () {progressBar. setVisibility (View. VISIBLE) ;}@ Overridepublic void hideProgress () {progressBar. setVisibility (View. GONE) ;}@ Overridepublic void setUsernameError () {username. setError (getString (R. string. username_error);} @ Overridepublic void setPasswordError () {password. setError (getString (R. string. password_error) ;}@ Overridepublic void navigateToHome () {startActivity (new Intent (this, MainActivity. class); finish () ;}@ Overridepublic void onClick (View v) {presenter. validateCredentials (username. getText (). toString (), password. getText (). toString ());}}

/*** Presenter interface, declares the login logic processing interface * @ author Quinn * @ date 2015-5-9 */public interface LoginPresenter {public void validateCredentials (String username, String password );}

/*** Specific implementation of Presenter, holding reference of View and Interator, processing of UI response, data processing, etc. * Where Interactor represents Model, you can Use different Interactor to process each Use case * @ author Quinn * @ date 2015-5-9 */public class implements LoginPresenter, OnLoginFinishedListener {private LoginView loginView; private LoginInteractor loginInteractor; public LoginPresenterImpl (LoginView loginView) {this. loginView = loginView; this. loginInteractor = new LoginInteractorImpl () ;}@ Overridepublic void validateCredentials (String username, String password) {loginView. showProgress (); loginInteractor. login (username, password, this) ;}@ Overridepublic void onUsernameError () {loginView. setUsernameError (); loginView. hideProgress () ;}@ Overridepublic void onPasswordError () {loginView. setPasswordError (); loginView. hideProgress () ;}@ Overridepublic void onSuccess () {loginView. navigateToHome ();}}

/*** Interface for processing login * @ author Quinn * @ date 2015-5-9 */public interface LoginInteractor {public void login (String username, String password, OnLoginFinishedListener listener );}


/*** Log on to the implementation of Interactor, hold its reference in Presenter * @ author Quinn * @ date 2015-5-9 */public class LoginInteractorImpl implements LoginInteractor {@ Override public void login (final String username, final String password, final OnLoginFinishedListener listener) {// Mock login. i'm creating a handler to delay the answer a couple of seconds new Handler (). postDelayed (new Runnable () {@ Override public void run () {Boolean error = false; if (TextUtils. isEmpty (username) {listener. onUsernameError (); error = true;} if (TextUtils. isEmpty (password) {listener. onPasswordError (); error = true;} if (! Error) {listener. onSuccess () ;}}, 2000 );}}


/*** Login result callback interface * @ author Quinn * @ date 2015-5-9 */public interface OnLoginFinishedListener {public void onUsernameError (); public void onPasswordError (); public void onSuccess ();}

Analyze the code interface above.

View layer: it is a layer. It mainly defines a View Interface, declares various methods for processing the UI component, and is implemented by the Activity. However, the final call is in the Presenter, when Presenter is defined in Activity, View is passed to Presenter as a constructor parameter.

Presenter layer: business layer, response UI Layer, data layer, logical processing, hold Interactor reference.

Model/Interactor layer: here the Interactor is the Model layer, which defines the specific behavior interface for Presenter to call and can implement different Interactor to process different Use cases. Such as Data Reading, data storage, and network requests.



References:

Http://antonioleiva.com/mvp-android/

Https://github.com/antoniolg/androidmvp



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.