Android MVP极限封装快乐十分采集器修复

MVP架构在Android快乐十分采集器修复,需要请搜索【大神源码论坛】dsluntan.com客服企娥3393756370 V信17061863513,这一块已经盛行依旧,对于一些学习能力比较强的人来说,已经能够运用自如甚至改造优化了,对于吾等菜鸟,却是如此的陌生,今日这篇博客,算是小弟在学习和应用上的一点总结罢了,如有不足,还请各位大神不吝指教。

MVP架构是什么就不多说了,博主主要很大家分享的是,如何设计MVP架构。

先来分析一下MVP如何使用:M-V-P三层之间,P作为中间层,负责M,V之间的数据交互的中介,将数据从M层获取,处理之后提交到V层,换句话说,V需要持有P的实例,P层需要持有V的实例。原理很简单,使用泛型对数据进行封装处理:
1.定义一个V层的空接口,主要是方便封装:

/**

  • V层接口
    */
    public interface IView {
    }

2.定义一个P层的接口:

/**

  • 抽象为接口
  • */
    public interface IPresenter<V extends IView> {

    /**

    • 绑定视图
    • @param view
      */
      void attachView(V view);

    /**

    • 解除绑定(每个V记得使用完之后解绑,主要是用于防止内存泄漏问题)
      */
      void dettachView();

}

3.封装P基类:绑定解绑V实例

/**

  • 抽象类 统一管理View层绑定和解除绑定
  • @param <V>
    */
    public class BasePresenter<V extends IView> implements IPresenter<V> {

    private WeakReference<V> weakView;
    protected M model;

    public V getView() {
    return proxyView;
    }

    /**

    • 用于检查View是否为空对象
    • @return
      */
      public boolean isAttachView() {
      return this.weakView != null && this.weakView.get() != null;
      }

    @Override
    public void attachView(V view) {
    this.weakView = new WeakReference<V>(view);
    }

    @Override
    public void dettachView() {
    if (this.weakView != null) {
    this.weakView.clear();
    this.weakView = null;
    }
    }
    }

4.M层封装:

/**

  • M层
    */
    public interface IModel {

}

/**

  • 登录model
  • Created by admin on 2018/2/5.
    */
    public interface ILoginModel extends IModel {
    void login(LoginCallback callback);//callback是登陆时的回掉
    }

/**

之后,将数据提交到activity或者fragment就行了。
最基本的铺垫已经做好了,接下来就该封装View了:

/**

  • Created by admin on 2018/2/5.
    */

public abstract class MvpActivity<V extends IView, P extends BasePresenter<V>> extends AppCompatActivity implements IView {

private P presenter;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ...
    presenter=getPresenter();
    presenter.attachView((V)this);
}

protected P getPresenter() {
    return presenter;
}

protected void setPresenter(P presenter) {
    this.presenter = presenter;
}

protected V getView() {
    return (V) this;
}
...
@Override
protected void onDestroy() {
    presenter.dettachView();
    ...
    super.onDestroy();
}

}

猜你喜欢

转载自blog.51cto.com/13974471/2176016