设计模式与架构04 -- MVP架构

2022-03-03  本文已影响0人  YanZi_33

MVC架构

MVP

MVC项目 -- 登录
public class User {
    private String name;
    private int age;

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

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}
public class MainActivity extends AppCompatActivity {

    private EditText userNameEt;
    private EditText passworEt;
    private ProgressDialog progressDialog;

    private Callback callback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameEt = findViewById(R.id.user_name);
        passworEt = findViewById(R.id.password);

        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("请稍后...");

        callback = new Callback() {
            @Override
            public void onSuccess(User user) {
                progressDialog.dismiss();
                System.out.println("网络请求成功");
                Toast.makeText(MainActivity.this,"成功!!!",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(String message) {
                System.out.println(message);
                Toast.makeText(MainActivity.this,"失败!!!",Toast.LENGTH_SHORT).show();
            }
        };
    }

    ///按钮的点击事件
    public void login(View view) {
        Log.e("MainActivity","登录");
        String user_name = userNameEt.getText().toString();
        String password = passworEt.getText().toString();

        if (user_name.isEmpty()) {
            Toast.makeText(MainActivity.this,"请输入用户名",Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.isEmpty()) {
            Toast.makeText(MainActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
            return;
        }

        progressDialog.show();
        //模拟登录网络请求
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                User user = new User("yanzi",30);
                callback.onSuccess(user);
            }
        },3000);
    }
}
public interface Callback {
    public void onSuccess(User user);
    public void onFailure(String message);
}
MVP -- 登录
import android.os.Handler;
import android.os.Looper;

public class LoginModel implements ILoginModel {
    @Override
    public void login(final String userName, final String password, final Callback callback) {
        //模拟耗时操作 网络请求
        new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
            @Override
            public void run() {
                User user = new User("yanzi",30);
                callback.onSuccess(user);
            }
        },3000);
    }
}
public interface ILoginModel {
    void login(String userName, String password, Callback callback);
}
import com.example.loginmvp.IView;
import com.example.loginmvp.model.Callback;
import com.example.loginmvp.model.ILoginModel;
import com.example.loginmvp.model.LoginModel;
import com.example.loginmvp.model.User;

public class LoginPresent implements ILoginPresenter{

    private IView iview;
    private ILoginModel iLoginModel;

    public LoginPresent(IView view) {
        this.iview = view;
        this.iLoginModel = new LoginModel();
    }

    @Override
    public void login(String userName, String password) {
        if (userName.isEmpty()) {
            iview.showWarnInfo("请输入用户名");
            return;
        }
        if (password.isEmpty()) {
            iview.showWarnInfo("请输入密码");
            return;
        }

        iview.showProgress();
        iLoginModel.login(userName, password, new Callback() {
            @Override
            public void onSuccess(User user) {
                iview.hideProgress();
                iview.loginSuccess(user);
            }

            @Override
            public void onFailure(String message) {
                iview.hideProgress();
                iview.loginFaliure(message);
            }
        });
    }
}
public interface ILoginPresenter {
    public void login(String userName,String password);
}
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.example.loginmvp.model.User;
import com.example.loginmvp.presenter.LoginPresent;

public class MainActivity extends AppCompatActivity implements IView {

    private EditText userNameEt;
    private EditText passworEt;
    private ProgressDialog progressDialog;

    private LoginPresent presenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameEt = findViewById(R.id.user_name);
        passworEt = findViewById(R.id.password);

        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("请稍后...");

        presenter = new LoginPresent(this);
    }

    public void login(View view) {
        Log.e("MainActivity","登录");
        String user_name = userNameEt.getText().toString();
        String password = passworEt.getText().toString();
        presenter.login(user_name,password);
    }

    @Override
    public void showProgress() {
        progressDialog.show();
    }

    @Override
    public void hideProgress() {
        progressDialog.hide();
    }

    @Override
    public void loginSuccess(User user) {
        Toast.makeText(MainActivity.this,"成功!!!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginFaliure(String message) {
        Toast.makeText(MainActivity.this,"失败!!!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showWarnInfo(String info) {
        Toast.makeText(MainActivity.this,info,Toast.LENGTH_SHORT).show();
    }
}
import com.example.loginmvp.model.User;

public interface IView {
    //显示Hud
    void showProgress();

    //隐藏hud
    void hideProgress();

    //登录成功
    void loginSuccess(User user);

    //登录失败
    void loginFaliure(String message);

    //提示信息
    void showWarnInfo(String info);
}
MVP的基类封装
public abstract class BasePresenter<T extends IBaseView> implements IBasePresenter<T> {

    protected T mView;

    @Override
    public void attachView(T view) {
        mView = view;
    }

    @Override
    public void detachView() {
        mView = null;
    }

    @Override
    public boolean isViewAttached() {
        return false;
    }
}
public interface IBasePresenter<T extends IBaseView> {

    /**
     * 依附生命view
     *
     * @param view
     */
    void attachView(T view);

    /**
     * 分离View
     */
    void detachView();

    /**
     * 判断View是否已经销毁
     *
     * @return
     */
    boolean isViewAttached();

}
public interface IBaseView {

}
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

///内部成员presenter 通过泛型 限制其类型
///接口支持继承
///class类支持继承

///<T extends IBasePresenter> T的类型为:实现了 继承自IBasePresenter接口的 类
///LoginPresent类 实现了ILoginPresenter接口 ILoginPresenter接口是继承自IBasePresenter接口的

///BaseMVPActivity的特征
//1.拥有一个 实现了继承自IBasePresenter接口 类型的成员 mPresenter 通过泛型
//2.实现了 IBaseView接口
//泛型在具体使用时 指定具体的类型
//继承关系 在具体使用时 父类指针可以指向子类对象
public abstract class BaseMVPActivity<T extends IBasePresenter> extends AppCompatActivity implements IBaseView {

    protected T mPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initPresenter();
        init();
    }

    protected void initPresenter() {
        mPresenter = createPresenter();
        //绑定生命周期 present的生命周期与view的生命周期同步
        if (mPresenter != null) {
            mPresenter.attachView(this);
        }
    }

    @Override
    protected void onDestroy() {
        if (mPresenter != null) {
            mPresenter.detachView();
        }
        super.onDestroy();
    }

    /**
     * 创建一个Presenter
     *
     * @return
     */
    protected abstract T createPresenter();

    protected abstract void init();
}
package com.example.loginmvp.presenter;

import com.example.base.BasePresenter;
import com.example.loginmvp.view.ILoginView;
import com.example.loginmvp.model.Callback;
import com.example.loginmvp.model.ILoginModel;
import com.example.loginmvp.model.LoginModel;
import com.example.loginmvp.model.User;

public class LoginPresenter extends BasePresenter<ILoginView> implements ILoginPresenter{

    private ILoginModel iLoginModel;

    public LoginPresenter() {
        this.iLoginModel = new LoginModel();
    }

    @Override
    public void login(String userName, String password) {
        if (userName.isEmpty()) {
            mView.showWarnInfo("请输入用户名");
            return;
        }
        if (password.isEmpty()) {
            mView.showWarnInfo("请输入密码");
            return;
        }

        mView.showProgress();
        iLoginModel.login(userName, password, new Callback() {
            @Override
            public void onSuccess(User user) {
                mView.hideProgress();
                mView.loginSuccess(user);
            }

            @Override
            public void onFailure(String message) {
                mView.hideProgress();
                mView.loginFaliure(message);
            }
        });
    }
}
package com.example.loginmvp.view;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.base.BaseMVPActivity;
import com.example.loginmvp.R;
import com.example.loginmvp.model.User;
import com.example.loginmvp.presenter.LoginPresenter;

public class MainActivity extends BaseMVPActivity<LoginPresenter> implements ILoginView {

    private EditText userNameEt;
    private EditText passworEt;
    private ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        userNameEt = findViewById(R.id.user_name);
        passworEt = findViewById(R.id.password);

        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setCancelable(false);
        progressDialog.setMessage("请稍后...");
    }

    @Override
    protected LoginPresenter createPresenter() {
        return new LoginPresenter();
    }

    @Override
    protected void init() {

    }

    public void login(View view) {
        Log.e("MainActivity","登录");
        String user_name = userNameEt.getText().toString();
        String password = passworEt.getText().toString();
        mPresenter.login(user_name,password);
    }

    @Override
    public void showProgress() {
        progressDialog.show();
    }

    @Override
    public void hideProgress() {
        progressDialog.hide();
    }

    @Override
    public void loginSuccess(User user) {
        Toast.makeText(MainActivity.this,"成功!!!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void loginFaliure(String message) {
        Toast.makeText(MainActivity.this,"失败!!!",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showWarnInfo(String info) {
        Toast.makeText(MainActivity.this,info,Toast.LENGTH_SHORT).show();
    }
}

总结

上一篇下一篇

猜你喜欢

热点阅读