MVP与MVP+Dagger2的使用及比较

时间:2023-01-18 21:14:36

MVP初体验与MVP引入Dagger2初体验

Mvp模式理解
一、分析Presenter层
1.1 Presenter 注入 实现接口IView的Acticity

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IUserLoginView {
...
private void loadData() {
mLoginPresenter = new LoginPresenter();
mLoginPresenter.attachView(this);
}
...
}

1.2 Presenter 构造中创建Model层

public class LoginPresenter extends BasePresenter<IUserLoginView> {
private LoginTasksRepository mLoginTasksRepository;

public LoginPresenter() {
mLoginTasksRepository = new LoginTasksRepository();
}

Login();--
}

1.3 Presenter 中处理当前Activity中的事件,如登陆事件Login
下图分析:调用model层方法Login,处理网络请求,返回结果,view层响应事件

public void login(String loginReq){
mLoginTasksRepository.login(loginReq, new LoginTaskDataSource.NetTasksCallback() {
@Override
public void onSuccess(LoginResponse loginResponse) {
mvpView.hideLoading();
mvpView.onSuccess(loginResponse);
}

@Override
public void onFailure(String errorMsg) {

}
});
}

public abstract class BasePresenter<V extends IMvpView> implements Presenter<V> {
protected V mvpView;

public void attachView(V view){
mvpView = view;
}

@Override
public void detachView(V view) {
mvpView = null;
}

@Override
public String getName() {
return mvpView.getClass().getSimpleName();
}
}

public interface Presenter<V> {
void attachView(V view);

void detachView(V view);

String getName();
}

二、分析Model层
2.1 网络请求或数据库存储

public interface LoginTaskDataSource {

void saveLoginResponse(LoginResponse loginResponse);

void login(String loginReq, NetTasksCallback callback);

interface NetTasksCallback{
void onSuccess(LoginResponse loginResponse);
void onFailure(String errorMsg);
}
}


public class LoginTasksRepository implements LoginTaskDataSource {
@Override
public void saveLoginResponse(LoginResponse loginResponse) {

}

@Override
public void login(String loginReq, NetTasksCallback callback) {
//开启网络请求

//成功后
LoginResponse loginResponse = new LoginResponse();
loginResponse.setResultInfo(loginReq+"===登录成功");
loginResponse.setResultCode("0");
callback.onSuccess(loginResponse);
}
}

三、分析View层
3.1 分析Activity中的具体方法,封装进接口,在loadData中注入。点击事件调用P层

public interface IMvpView {
void onError(String errorMsg, String code);

void onSuccess(LoginResponse loginResponse);

void showLoading();

void hideLoading();
}

public interface IUserLoginView extends IMvpView{
void clearEditContent();
}

public class MainActivity extends AppCompatActivity implements View.OnClickListener, IUserLoginView {

private EditText mUsername;
private EditText mPassword;
private Button mLogin;
private LoginPresenter mLoginPresenter;

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

initView();
loadData();
}

private void initView() {
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
mLogin = (Button) findViewById(R.id.login);

mLogin.setOnClickListener(this);
}

private void loadData() {
mLoginPresenter = new LoginPresenter();
mLoginPresenter.attachView(this);
}

@Override
public void onClick(View v) {
mLoginPresenter.login(mUsername.getText().toString() + "-" + mPassword.getText().toString());
}

@Override
public void clearEditContent() {
mUsername.setText("");
mPassword.setText("");
}

@Override
public void onError(String errorMsg, String code) {

}

@Override
public void onSuccess(LoginResponse loginResponse) {
Toast.makeText(this, loginResponse.getResultInfo(), Toast.LENGTH_SHORT).show();
}

@Override
public void showLoading() {

}

@Override
public void hideLoading() {

}

@Override
protected void onDestroy() {
mLoginPresenter.detachView(this);
super.onDestroy();
}
}

3.2 当Destory时,需注销GC链,避免内存泄露

Mvp+ Dagger2模式理解

依赖

apply plugin: 'com.neenbedankt.android-apt'
dependencies:
apt 'com.google.dagger:dagger-compiler:2.2'
compile 'com.google.dagger:dagger:2.2'

一、分析Presenter层
分析:一样也是P连接 M 和 V

public class MainPresenter implements MainContract.IPresenter {
private MainContract.IModel model;
private MainContract.IView view;

public MainPresenter(MainContract.IView view) {
this.view = view;
this.model = new MainModel();
}

@Override
public void onButtonClicked(Context context, String text) {
model.onButtonClicked(context,text);
}
}

二、mvp接口类

public class MainContract {

interface IView{
void onButtonClicked(String text);
}

interface IModel{
void onButtonClicked(Context context,String text);
}

interface IPresenter{
void onButtonClicked(Context context,String text);
}
}

三、Model层

public class MainModel implements MainContract.IModel {
@Override
public void onButtonClicked(Context context, String text) {
Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
}
}

四、注入接口

@Singleton
@Component(modules = MainModule.class)
public interface MainComponent {
void inject(MainActivity activity);
}

五、引入dagger2模式
分析:对应第六点的注入过程,给V提供P层对象

@Module
public class MainModule {
MainPresenter mPresenter;

public MainModule(MainActivity activity){
mPresenter = new MainPresenter(activity);
}
@Provides
@Singleton
MainPresenter providersMainPresenter(){
return mPresenter;
}
}

六、View
分析:实现IView 接口,DaggerMainComponent注入(相对于MVP少了新建P层,调P层方法注入),点击调P层

public class MainActivity extends AppCompatActivity implements MainContract.IView {
@Inject
MainPresenter presenter;

@InjectView(R.id.btn)
Button mBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
//这步之前需要rebuild一下
DaggerMainComponent.builder().mainModule(new MainModule(MainActivity.this)).build().inject(this);
}

@Override
public void onButtonClicked(String text) {
presenter.onButtonClicked(this,text);
}

@OnClick(R.id.btn)
public void onClick() {
onButtonClicked("Button Clicked!!!");
}
}

两者之间的比较

简单的说,单纯使用MVP,代码结构清晰,注入Dagger2后,代码交接成本高,当然耦合是降低了,个人比较推荐使用MVP的。

感谢

MVP的学习一直都有,最近有时间,做一下总结,方便下个项目的使用,在这里感谢曹银飞顾林海等人的博客,对我的帮助挺大的。由于版权申明,不能转载博客。感谢大大。