Android MVP极限封装(一)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z2464342708m/article/details/79374854
                                        <div class="markdown_views">
            <p>MVP架构在Android这一块已经盛行依旧,对于一些学习能力比较强的人来说,已经能够运用自如甚至改造优化了,对于吾等菜鸟,却是如此的陌生,今日这篇博客,算是小弟在学习和应用上的一点总结罢了,如有不足,还请各位大神不吝指教。</p>

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

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

/**
 * V层接口
 */
public interface IView {
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5

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

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

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

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

}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

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;
        }
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36

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 class LoginModel implements ILoginModel {
    @Override
    public void login() {
        // TODO: 2018/2/5 发起登录请求 
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25

之后,将数据提交到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(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();
    }
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

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

public interface ILoginView extends IView {

    void onLoginSuccess();
    void onFailed();

}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

然后是Presneter:

/**
 * Created by admin on 2018/2/5.
 */
public class LoginPresenter extends BasePresenter<ILogin, LoginModel> {

    public LoginPresenter() {
        model = new LoginModel();
    }

    public void login(){
        model.login(new LoginCallBack() {
            @Override
            public void onSuccess() {
                if(null!=(ILogin)getView()){
                    weakView.onLoginSuccess();
                }
            }

            @Override
            public void onFailure() {
                if(null!=(ILogin)getView()){
                    weakView.onFailure();
                }
            }
        });
    }

}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

最后来完成Activity的逻辑:

public class LoginActivity extends MvpActivity<ILoginView, LoginPresenter> implements ILoginView {
    ...
    @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(){

    ...
}
  
  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

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



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值