Dagger基本介绍
根据官网来说他就是一个快速的依赖注入框架,作用重点就是解耦和管理实例对象,MVP模式可以在MVP模式上进一步解耦
Dagger2注解简单使用(以购物为例手机"User"和"Book")
直销店如小米手机销店user
public class User {
private String name;
@Inject //标记产品对象,没有构造参数不需要Module
public User(){ this.name = "Sir1";}
public String getName(){return name;}
}
组装手机,有构造参数需要Module不能用@Inject
public class Book {
private String name;
public Book(String name){this.name = name;}
public String getName() { return name;}
}
有些手机专卖店买手机,有组装的手机也有直销的手机这时
@Module //表示这个是手机专卖店
public class TestModule {
@Provides //提供各种各样的组装机1
Book provideBook(){return new Book("book1"); }
//@Provides //提供各种各样的组装机2
// Book provideXiaomi(){return new Xiaomi("book2"); }
}
写好邮件地址
//@Component //表示标记要购买的物品如上的User对象
@Component(modules = {TestModule.class}) //标记手机专卖店当然也标记直销店
public interface TestComponent {
void inject(MainActivity activity);
}
用户地址确认
public class MainActivity extends AppCompatActivity {
@Inject //标记直销机User
User mUser;
@Inject //标记组装机Book
Book mBook;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textView = findViewById(R.id.tv_1);
//确认收到货物获取手机对象mUser和mBook
DaggerTestComponent.create().inject(this);
textView.setText("用户名:"+mUser.getName()+"\n书 名:"+ mBook.getName());
}
}
到此Dagger2简单使用完毕
Dagger2的MVP模式讲解
MVP基本概念
view层接受用户请求,将请求传给presenter,presenter获得请求将请求数据传给mode,mode层获取相关数据,就会将获得的数据通知presenter,presenter再通知view层更新数据,再通知用户
总的来说,mode层和view已经完全解耦,都是通过presenter建立起来,那么没有解耦的只能通过Dagger来实现 , 为mode与presenter ,view与presenter
那么presenter与view怎么实现通知的?
通过view层实现接口,再在presenter调用接口方法
接口如下
public interface IUserBookShowListener {
void getUserBookSuccess(Book book);
void getUserBookFailed(String book);
}
定义基类
public abstract class BasePresenter<V> {
protected WeakReference<V> weakRefView;
public void attachView(V view){
weakRefView = new WeakReference<V>(view);
}
public void detach() {
if(isAttach()) {
weakRefView.clear();
weakRefView = null;
}
}
public V obtainView(){
return isAttach()?weakRefView.get():null;
}
protected boolean isAttach() {
return weakRefView != null &&
weakRefView.get() != null;
}
public abstract void onDestroy();
}
public abstract class BaseMVPActivity<V,P extends BasePresenter> extends AppCompatActivity {
private P mPresenter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPresenter = createPresenter();
mPresenter.attachView(this);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mPresenter !=null){
mPresenter.detach();
mPresenter.onDestroy();
mPresenter = null;
System.gc();
}
}
protected abstract P createPresenter();
}
view层代码
public class MainActivity extends BaseMVPActivity<UserBookPresenter> implements IUserBookShowListener{
private TextView tv_1;
private Button bt_1;
private EditText mEt_1;
@Inject //标记
UserBookPresenter mUserBookPresenter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
mEt_1 = findViewById(R.id.et_1);
bt_1 = findViewById(R.id.bt_1);
tv_1 = findViewById(R.id.tv_1);
bt_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (!TextUtils.isEmpty(mEt_1.getText())) {
String s = mEt_1.getText().toString();
try {
Integer integer = Integer.valueOf(s);
mUserBookPresenter.getUserBook(integer);
}catch (Exception e){
Toast.makeText(MainActivity.this, "请输入数字",
Toast.LENGTH_SHORT).show();
}
}
}
});
}
//实现接口
@Override
public void getUserBookSuccess(final Book book) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv_1.setText(book.getName());
}
});
}
//实现接口
@Override
public void getUserBookFailed(final String book) {
runOnUiThread(new Runnable() {
@Override
public void run() {
tv_1.setText(book);
}
});
}
//获取UserBookPresenter对象
@Override
public UserBookPresenter getPresenter() {
DaggerUserBookComponet.builder().userBookModule(new UserBookModule(this)).build().inject(this);
return mUserBookPresenter;
}
}
UserBookPresenter的实现
public class UserBookPresenter extends BasePresenter<IUserBookShowListener> {
@Inject //标记注入对象
UserBookMode mUserBookMode;
private IUserBookShowListener iUserBookShowListener;
public UserBookPresenter( Context iUserBookShowListener) {
this.iUserBookShowListener = obtainView();
//确认获取对象mUserBookMode实例
DaggerUserBookPresenterComponet.create().inject(this);
}
public void getUserBook(final int i) {
mUserBookMode.getUserBook(i, new OnUserBookListener() {
@Override
public void getUserBookSuccess(Book book) {
if (iUserBookShowListener==null){
return;
}
iUserBookShowListener.getUserBookSuccess(book);
}
@Override
public void getUserBookFailed(String book) {
if (iUserBookShowListener==null){
return;
}
iUserBookShowListener.getUserBookFailed(book);
}
});
}
}