android MVP架构学习

来源:互联网 发布:薛之谦淘宝女装店 编辑:程序博客网 时间:2024/06/05 06:43

MVP架构是(Model-View-Presenter)模型-试图-主导器模式

传统MVC在android开发中应用比较少了,耦合度太高,不利于维护

我已一个简单的例子来介绍一下MVP模式的实际应用

假设我要做一个登陆的功能

登陆的界面需要搭建,我就不掩饰了

把与页面相关的逻辑留在activity中

其他逻辑则放入presenter中处理

我来创建两个接口,一个是activity的相关逻辑,一个是presenter的相关逻辑

public interface IMainActivity {    public void loginSuccess();    public void loginFailed(String msg);}


public interface IMainActivityPresenter {    public void login(String phone, String password);}


我让activity和presenter类都实现对应的接口

public class MainActivity extends AppCompatActivity implements IMainActivity {    private EditText edtPhone, edtPassword;    private TextView txt;    private IMainActivityPresenter presenter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        presenter = new MainActivityPresenter(this);        edtPhone = (EditText) findViewById(R.id.edt_phone);        edtPassword = (EditText) findViewById(R.id.edt_password);        txt = (TextView) findViewById(R.id.txt);        findViewById(R.id.btn_commit).setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                login();            }        });    }    private void login() {        String phone = edtPhone.getText().toString();        String password = edtPassword.getText().toString();        presenter.login(phone, password);    }    @Override    public void loginSuccess() {        txt.setText("login success");    }    @Override    public void loginFailed(String msg) {        txt.setText("login failed " + msg);    }}

public class MainActivityPresenter implements IMainActivityPresenter {    private IMainActivity mainActivity;    private Handler handler = new Handler(new Handler.Callback() {        @Override        public boolean handleMessage(Message message) {            int what = message.what;            switch (what) {                case 1: {                    mainActivity.loginSuccess();                }                break;                case 2:{                    String errorMsg = (String) message.obj;                    mainActivity.loginFailed(errorMsg);                }                break;            }            return true;        }    });    public MainActivityPresenter(IMainActivity mainActivity) {        this.mainActivity = mainActivity;    }    private boolean isLoginSuccess = false;    @Override    public void login(String phone, String password) {        new Thread(new Runnable() {            @Override            public void run() {                isLoginSuccess = !isLoginSuccess;                try {                    Thread.sleep(2000);                } catch (InterruptedException e) {                    e.printStackTrace();                }                Message msg = handler.obtainMessage();                if (isLoginSuccess) {                    msg.what = 1;                    handler.sendMessage(msg);                } else {                    msg.what = 2;                    msg.obj = "wrong error";                    handler.sendMessage(msg);                }            }        }).start();    }}

我的presenter简单模拟了一下异步处理
在activity中创建presenter对象,presenter也持有activity对象(注意此处处理不好,容易造成内存泄漏)

    @Override    protected void onDestroy() {        super.onDestroy();        presenter.release();        presenter = null;    }





0 0
原创粉丝点击