Android MVP

MVP

M(model)负责数据的请求,解析,过滤等数据操作

V(View)负责图示部分展示,图示事件处理 

P(presenter)是View和Model交互的桥梁。


优点: 单一职责, Model, View, Presenter只处理单一逻辑,解耦,Model层的修改和View层的修改互不影响

面向接口编程,依赖抽象,Presenter和View互相持有抽象引用,对外隐藏内部实现细节。

缺点:Model进行一步操作的时候,获取结果通过Presenter会传到View的时候,出现View引用的空指针异常。Presenter和View相互持有引用,解除不及时的话容易出现内存泄漏。会产生很多的类,但是会很大程序的简洁view中的代码。

public class MainActivity extends AppCompatActivity implements MainContract.view {
    MainPresenter mainPresenter;
    PostModule pModule;
    GetModule gModule;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mainPresenter = new MainPresenter(this, this);
        mainPresenter.getModule(0);
        mainPresenter.getPostModule(0);
    }

    @Override
    public void showPostModule(PostModule postModule) {
        pModule = postModule;
    }

    @Override
    public void showGetModule(GetModule getModule) {
        gModule = getModule;
    }
}
public class MainPresenter implements MainContract.Presenter {
    private MainContract.view view;
    private Context context;

    public MainPresenter(MainContract.view view, Context context) {
        this.view = view;
        this.context = context;
    }

    @Override
    public void getPostModule(int id) {
        RxHttp.getInstance().getPostModule(new HttpSubscriber<PostModule>(new SubscriberOnNextListener<PostModule>() {
            @Override
            public void onSuccess(PostModule postModule) {
                view.showPostModule(postModule);
            }

            @Override
            public void onError(int code, String errorMsg) {

            }
        }), id);
    }

    @Override
    public void getModule(int id) {
        RxHttp.getInstance().getModule(new HttpSubscriber<GetModule>(new SubscriberOnNextListener<GetModule>() {
            @Override
            public void onSuccess(GetModule getModule) {
                view.showGetModule(getModule);
            }

            @Override
            public void onError(int code, String errorMsg) {

            }
        }), id);
    }
}
public interface MainContract {

    interface view {
        void showPostModule(PostModule postModule);

        void showGetModule(GetModule getModule);
    }

    interface Presenter {
        void getPostModule(int id);

        void getModule(int id);
    }

}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值