MVP架构设计 进阶一

MVP架构设计进阶

public abstract class MvpLinearLayout<V extends MvpView,P extends MvpPresenter<V>> extends LinearLayout {

    private P presenter;
    private V view;
    public MvpLinearLayout(Context context) {
        super(context);
    }

    public MvpLinearLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }
    private void init() {
        if (this.presenter == null) {
            this.presenter = createPresenter();
        }
        if (this.view == null) {
            this.view = createView();
        }
        if (this.presenter != null && this.view != null) {
            this.presenter.attachView(view);
        }
    }
    /**
     * 绑定P层
     *
     * @return
     */
    public abstract P createPresenter();

    /**
     * 创建View层
     *
     * @return
     */
    public abstract V createView();

    @Override
    public void removeView(View view) {
        super.removeView(view);
    }

   //从屏幕上移除
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (this.presenter != null) {
            this.presenter.dettachView();
            this.presenter = null;
        }
    }

}
复制代码
public abstract class MvpActivity<V extends MvpView, P extends MvpPresenter<V>> extends Activity {

    private P presenter;
    private V view;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (this.presenter == null) {
            this.presenter = createPresenter();
        }
        if(this.view == null){
            this.view = createView();
        }
        if(this.presenter !=null && this.view !=null){
            this.presenter.attachView(view);
        }
    }

    public P getPresenter() {
        return presenter;
    }

    public V getView() {
        return view;
    }

    /**
     * 绑定P层
     *
     * @return
     */
    public abstract P createPresenter();

    /**
     * 创建View层
     *
     * @return
     */
    public abstract V createView();

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(this.presenter !=null){
            this.presenter.dettachView();
            this.presenter =null;
        }
    }
}
复制代码
public abstract class MvpFragment<V extends MvpView,P extends MvpPresenter<V>> extends Fragment{
    private P presenter;
    private V view;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        if(this.presenter == null){
            this.presenter = createPresenter();
        }
        if(this.view == null){
            this.view = createView();
        }
        if(this.presenter!=null && this.view !=null){
            this.presenter.attachView(view);
        }
    }
    public abstract P createPresenter();

    /**
     * 创建View
     * @return
     */
    public abstract V createView();

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (this.presenter != null) {
            this.presenter.dettachView();
            this.presenter = null;
        }
    }

}
复制代码

进阶第一步:分析MvpFragment、MvpActivity、MvpLinearLayout ...... 问题一: 每一个模块的Mvp基类,都有相同定义的方法。 解决方案:再抽象(抽象:抽象类、接口) 注意:需要使用一个Java特殊语法(接口多态)

                   问题二:每一个模块Mvp基类都要去实现绑定过程,这样一个过程就会导致代码冗余,同时不方便维护升级(例如:MvpFragment、MvpActivity、MvpLinearLayout ......)
                   解决方案:代理模式 — 静态代理
                   
                  问题三(场景):Activity很有可能意外关闭,而这个时候我们Mvp架构中的数据有需要保存,这个时候我们目前的框架是无法满足这个要求的。--- 需要进一步的优化
                  解决方案:代理模式 — 静态代理Activity
                  目标接口:定义Activity目标接口
                  具体的目标接口:
复制代码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值