(Unfinished) [MVP] Android mode shown signs (a)

Guo Lin recently when reading a large number of public God, classification schema caught my attention.

While individual developers (the level of vegetable kind), but will eventually be closer to a formal project developer. So next time, mainly to learn about the MVP architecture, Retrofit and RxJava

This article from study_zhxu, blog address: http: //blog.csdn.net/study_zhxu

This article is not complete, do only learning record

 


The UI logic MVP Activity View abstract interfaces into the business logic into Presenter abstract interfaces, Model class or the original Model.
In the MVP mode, Activity function is to respond to the life cycle and display interface, other specific work thrown Presenter layers were complete, in fact, is a bridge Model Presenter View layer and layer.

 

 

 

 


Creating MVP steps:
1. Create IPresenter interfaces, all business logic interfaces on here, and it was created to achieve PresenterCompl (where you can easily see the business logic function, because the interface can be implemented in many so it is easy to write unit tests )
2. Create IView interface, a logical interface to all views are placed here, its implementation is the current class the Activity / Fragment
3.Aactivity contains only a IPresenter, and in turn contains a PresenterCompl IView and relied Model. Activity was retained only call IPresenter, and all other work left to PresenterCompl implemented.
4.Model not be there, but there will be View and Presenter.

 

Directly on the examples:

1. Create a Presenter Interface

ILoginPresenter

package com.paul.learningmvp.presenter;

public  interface ILoginPresenter {
     // clear the input box 
    public  void Clear ();
     // login operation 
    public  void doLogin (String name, String password);
}

Achieve ILoginPresenteeer

LoginPresenterCompl

package com.paul.learningmvp.presenter;

import com.paul.learningmvp.model.User;
import com.paul.learningmvp.view.ILoginView;


public class LoginPresenterCompl implements ILoginPresenter {

    private ILoginView loginView;
    private User user;

    public LoginPresenterCompl(ILoginView loginView) {
        this.loginView = loginView;
        user=new User("110","123456");
    }

    @Override
    public void clear() {
        loginView.onClearText();
    }

    @Override
    public void doLogin(String name, String password) {
        boolean result=false;
        int code=0;
        if(name.equals(user.getName())&&password.equals(user.getPassword())){
            result=true;
            code=1;
        }else {
            result=false;
            code=0;
        }
        loginView.onLoginResult(result,code);
    }
}

2. Create IView interfaces, all write operations on the View of here, and related Presenter

ILoginView

package com.paul.learningmvp.view;

public interface ILoginView {
    public void onClearText();
    public void onLoginResult(Boolean result,int code);
}

Activit is a class that implements IView interface

LoginActivity

package com.paul.learningmvp.view;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.paul.learningmvp.R;
import com.paul.learningmvp.presenter.ILoginPresenter;
import com.paul.learningmvp.presenter.LoginPresenterCompl;
import com.paul.learningmvp.view.ILoginView;

public class LoginActivity extends AppCompatActivity
                                implements ILoginView, View.OnClickListener {

    private Button mLogin;
    private Button mClear;
    private EditText mName;
    private EditText mPassword;
    ILoginPresenter loginPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mLogin=findViewById(R.id.btn_confirm);
        mClear=findViewById(R.id.btn_clear);
        mName=findViewById(R.id.tv_name);
        mPassword=findViewById(R.id.tv_password);

        mLogin.setOnClickListener(this);
        mClear.setOnClickListener(this);
        loginPresenter=new LoginPresenterCompl(this);
    }

    @Override
    public void onClearText() {
        mName.setText("");
        mPassword.setText("");
        Toast.makeText(this,"clear",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onLoginResult(Boolean result, int code) {
        mLogin.setEnabled(true);
        mClear.setEnabled(true);
        if(result){
            Toast.makeText ( the this , "login success" , Toast.LENGTH_SHORT) .Show ();
        }else {
            Toast.makeText ( the this , "login failed" , Toast.LENGTH_SHORT) .Show ();
        }
    }

    @Override
    public void onClick(View v) {
        String name=mName.getText().toString();
        String password=mPassword.getText().toString();
        switch (v.getId()){
            case R.id.btn_confirm:
                loginPresenter.doLogin(name,password);
                break;
            case R.id.btn_clear:
                loginPresenter.clear();
                break;
        }
    }
}

3.model

User

package com.paul.learningmvp.model;

public class User {
    private String name;
    private String password;

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/robotpaul/p/11864927.html