Android MVP极限封装菠菜程序定制开发

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

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, M extends IModel> 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();
    }

/**

/**

  • 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(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();
}

}
收工,MVP基础框架搭建完成了。没错,就是基础框架,但是能不能用呢,让我们拭目以待吧。
先来写一个View:

public interface ILoginView extends IView {

void onLoginSuccess();
void onFailed();

}
然后是Presneter:

/**

}
最后来完成Activity的逻辑:

public class LoginActivity extends MvpActivity<ILoginView, LoginPresenter> implements ILoginView {br/>...
@Override
public LoginPresenter getPresenter() {
return new LoginPresenter();
}

public void login(View view) {
    String name = etUserName.getText().toString();
    String pwd = etUserPwd.getText().toString();
    getPresenter().login(name, pwd);
}

@Override
public void onLoginSuccess() {

}

@Override
public void onFailed(){

...

}

博客只简单的介绍了如何去封装MVP架构,但是还存在着不完美之处,MVP架构优化目前发现了一个问题,如果各位还有什么发现,记得联系我哦,一起努力完善。

猜你喜欢

转载自blog.51cto.com/13964547/2173694