MVP框架搭建

MVP学过Android的至少应该略知一二,废话不说,直接上代码

View层

public abstract class BaseView

extends AppCompatActivity { protected P p; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); p = getPresenter(); // 绑定view p.bindView(this); } // 让P层做什么需求 public abstract CONTRACT getContract(); // 从子类中获取具体的Persenter public abstract P getPresenter(); @Override protected void onDestroy() { super.onDestroy(); // 解绑View p.unBindView(); } }

Presenter层

public abstract class BasePresenter {

    protected M m;

    public BasePresenter() {
        this.m = getModel();
    }

    // P层持有View层的弱引用
    protected WeakReference mWeakReference;

    public void bindView(V v) {
        mWeakReference = new WeakReference(v);
    }

    public void unBindView() {
        if (mWeakReference != null) {
            mWeakReference.clear();
            mWeakReference = null;
            System.gc();
        }
    }

    // 获取View
    public V getView() {
        if (mWeakReference != null) {
            return mWeakReference.get();
        }
        return null;
    }

    public abstract M getModel();

    // Model层与View层协商的业务
    public abstract CONTRACT getContract();

}

Model层

public abstract class BaseModel

{ protected P p; public BaseModel(P p) { this.p = p; } // Model层做的事情 public abstract CONTRACT getContract(); }

案例–登录功能

约定

public interface LoginContract {
    interface Model {
        // Model层子类完成方法的具体实现
        void executeLogin(String name, String pwd) throws Exception;
    }

    interface View {
        // 结果返回后,View层进行结果的处理
        void handleResult(T t);
    }

    interface Presenter {
        // 请求,从View层发出的指令
        void requestLogin(String name, String pwd);
        // 结果响应 (接收到Model层处理的结果,通知View层刷新)
        void responseResult(T t);
    }
}

View层

public class LoginActivity extends BaseView> {

    private EditText etName;
    private EditText etPwd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName = findViewById(R.id.et_name);
        etPwd = findViewById(R.id.et_pwd);
    }

    @Override
    public LoginContract.View getContract() {
        return new LoginContract.View() {
            @Override
            public void handleResult(UserInfo userInfo) {
                if (userInfo != null) {
                    Toast.makeText(LoginActivity.this, "登录成功" + userInfo.toString(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(LoginActivity.this, "登录失败...", Toast.LENGTH_SHORT).show();
                }
            }
        };
    }

    @Override
    public LoginPresenter getPresenter() {
        return new LoginPresenter();
    }

    public void doLogin(View view) {
        String name = etName.getText().toString();
        String pwd = etPwd.getText().toString();
        p.getContract().requestLogin(name, pwd);
    }

}

Presenter层

public class LoginPresenter extends BasePresenter> {


    @Override
    public LoginModel getModel() {
        return new LoginModel(this);
    }

    @Override
    public LoginContract.Presenter getContract() {
        return new LoginContract.Presenter() {
            @Override
            public void requestLogin(String name, String pwd) {
                try {
                    m.getContract().executeLogin(name, pwd);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void responseResult(UserInfo userInfo) {
                getView().getContract().handleResult(userInfo);
            }
        };
    }
}

Model层

public class LoginModel extends BaseModel {

    public LoginModel(LoginPresenter loginPresenter) {
        super(loginPresenter);
    }

    @Override
    public LoginContract.Model getContract() {
        return new LoginContract.Model() {
            @Override
            public void executeLogin(String name, String pwd) throws Exception {
                if (name.equals("lichaojun") && pwd.equals("123")) {
                    p.getContract().responseResult(new UserInfo("lcj", "lcj"));
                } else {
                    p.getContract().responseResult(null);
                }
            }
        };
    }
}

你可能感兴趣的:(Android)