之前写过一篇关于Dagger2入门的一篇文章。前一篇文章只是简单的介绍了Dagger2的注解和使用,本篇文章旨在探讨Dagger2实际运用于MVP开发模式中如何解耦的问题。接下来,我们一起去看看Dagger2实际上手是如何使用的。
创建Base M、V、P层
@Inject是不能使用在抽象类的构造函数上的,所以在解耦合MVP时需要注意。
每个人对MVP开发的模式都有不同的理解,那么在我看来,黑猫白猫,只要能抓老鼠的猫就是好猫。在java编程思想中,鼓励我们使用接口来解耦合,但是又防止滥用接口。在MVP模式中,很多开发人员都是通过接口去管理View层。如果是套路式的开发,那么这样做是没有问题的,大致的业务场景熟知,能在base层进行封装,或者说调整、改动的接口不多。但是,新的行业或者产品,不确定因素太多,那么基类中可封装的接口就有一定的局限性,导致不停的修改基类来抽取接口。
我理解的MVP模式是把Activity 或者Fragment当做View层,通过泛型和Dagger结合来解耦合Presenter层和Model层。
- View层
public abstract class BaseActivity<P extends BasePresenter> extends AppCompatActivity {
@Inject
protected P mPresenter;
private Activity mActivity;
protected ProgressDialog mProgressDialog;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) throws RuntimeException {
super.onCreate(savedInstanceState);
bindingDagger2(savedInstanceState);
if (this.mPresenter == null) {
throw new RuntimeException("没有设置Presenter或者没有重写onCreate()方法");
} else {
mActivity = this;
this.mPresenter.setActivity(this);
this.mPresenter.onCreate(savedInstanceState);
mProgressDialog = new ProgressDialog(this);
}
}
protected abstract void bindingDagger2(Bundle savedInstanceState);
protected void showDialog(){
mProgressDialog.setMessage("正在加载。。。");
mProgressDialog.show();
}
protected void hideDialog(){
if (mProgressDialog.isShowing()) {
mProgressDialog.dismiss();
}
}
}
1.通过@Inject将Presenter注入到Activity中,申明一个Presenter的实例。
2.将当前的Activity set到Presenter中。
- Presenter层
public abstract class BasePresenter<A extends BaseActivity, M extends BaseModel> {
protected A mActicity;
@Inject
protected M mModel;
public void setActivity(A acticity) {
this.mActicity = acticity;
}
@Inject
public void setPresenter() {
this.mModel.setPresenter(this);
}
public void onCreate(Bundle savedInstanceState) {
}
}
1.通过@Inject将Model注入到Presenter中,申明一个Model的实例.
2.通过@Inject注入setPresenter()方法,将model与之关联。
- Model层
public abstract class BaseModel<P extends BasePresenter> {
protected P mPresenter;
public void setPresenter(P presenter) {
mPresenter = presenter;
}
}
实现
- Activity
public class TestActivity extends BaseActivity<TestActivityPresenter> {
@BindView(R.id.b_net)
Button mBNet;
@BindView(R.id.tv_home)
TextView mTvHome;
@Override
protected void bindingDagger2(Bundle savedInstanceState) {
DaggerTestActivityComponent.create().inject(this);
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) throws RuntimeException {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
}
@OnClick(R.id.b_net)
public void onViewClicked() {
showDialog();
//模拟网络请求
new Thread(() -> {
try {
Thread.sleep(2000);
mPresenter.doNetWorking();
} catch (InterruptedException e) {
e.printStackTrace();
}
}).start();
}
public void showMsg(String subject) {
runOnUiThread(() -> {
Toast.makeText(TestActivity.this, subject, Toast.LENGTH_SHORT).show();
hideDialog();
});
}
public void bindViewHomeData(TestData.Data data) {
runOnUiThread(() -> {
mTvHome.setText(data.getSubject());
hideDialog();
});
}
}
- Presenter
public class TestActivityPresenter extends BasePresenter<TestActivity, TestActivityModel> {
private static final String TAG = "TestActivityPresenter";
@Inject
public TestActivityPresenter() {
}
public void doNetWorking() {
// 模拟网络请求返回的数据
List<TestData> list = new ArrayList<>();
TestData testData1 = new TestData();
TestData.Data data1 = new TestData.Data();
data1.setSubject("广告栏数据");
data1.setPicUrl("http://www.baidu.com/");
testData1.setData(data1);
testData1.setType("banner");
TestData testData2 = new TestData();
TestData.Data data2 = new TestData.Data();
data2.setSubject("主页数据");
data2.setPicUrl("http://www.baidu.com/");
testData2.setData(data2);
testData2.setType("home");
list.add(testData1);
list.add(testData2);
//处理数据
mModel.parseData(list);
}
public void onBannerData(TestData.Data banner) {
String subject = banner.getSubject();
mActicity.showMsg(subject);
}
public void onHomeData(TestData.Data home) {
String subject = home.getSubject();
String picUrl = home.getPicUrl();
mActicity.bindViewHomeData(home);
}
}
- Model
public class TestActivityModel extends BaseModel<TestActivityPresenter> {
private String banner = "banner";
private String home = "home";
@Inject
public TestActivityModel() {
}
public void parseData(List<TestData> list) {
// 处理完数据返回给Presenter层
for (TestData testData : list) {
String type = testData.getType();
TestData.Data data = testData.getData();
if (type.equals(banner)) {
mPresenter.onBannerData(data);
} else if (type.equals(home)) {
mPresenter.onHomeData(data);
} else {
// 其他。。。
}
}
}
}
完整代码上传至github。