Rx系列学习之旅(一)--MVP架构篇

一:MVP实现逻辑图:

注解:

  View:负责绘制UI元素、与用户进行交互(在Android中体现为Activity)

  Model:负责存储、检索、操纵数据(有时也实现一个Modelinterface用来降低耦合)

  Presenter:作为View与Model交互的中间纽带,处理与用户交互的负责逻辑

  View interface:需要View实现的接口,View通过Viewinterface与Presenter进行交互,降低耦合,方便进行单元测试

二:架构MVP框架:

1.分包

 model、

 view、

 presenter(contract)

2.先处理baseview、basepresenter

public interface BaseView {
    void showLoading();
    void  showError(String msg);
    void  dismissLoading();
}
public interface BasePresenter{

}
3.处理 contract契约类

public interface RecommendContract {
    interface View extends BaseView {
        void  showResult(List<AppInfo> datas);
        void showNodata();
    }
    interface Presenter extends BasePresenter{
        void requestDatas();
    }
}
4.处理 model数据层

public class RecommendModel {
    private  ApiService mApiService;
    public RecommendModel(ApiService apiService){
        this.mApiService  =apiService;
    }
    public  void getApps(Callback<PageBean<AppInfo>> callback){
        mApiService.getApps("{'page':0}").enqueue(callback);
    }
}
5.处理 presenter

public class RecommendPresenter implements RecommendContract.Presenter {
    private RecommendModel mModel;
    private RecommendContract.View mView;
    public RecommendPresenter(RecommendContract.View mView,RecommendModel model) {
        this.mView = mView;
        mModel = model;
    }
    @Override
    public void requestDatas() {
        mModel.getApps(new Callback<PageBean<AppInfo>>() {
            @Override
            public void onResponse(Call<PageBean<AppInfo>> call, Response<PageBean<AppInfo>> response) {
                if(response !=null){
                    mView.showResult(response.body().getDatas());
                }
                else{
                    mView.showNodata();
                }
            }
            @Override
            public void onFailure(Call<PageBean<AppInfo>> call, Throwable t) {
                mView.showError(t.getMessage());
            }
        });
    }
}
6.处理view层

public class WelcomeActivity extends AppCompatActivity implements RecommendContract.View {
    @Inject//dagger2的依赖注入
    RecommendContract.Presenter mPresenter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        DaggerRecommendCompenent.builder().appCompenent(((MyApplication)getApplication()).getAppCompenent())
             .recommendModule(new RecommendModule(this)).build().inject(this);
        mPresenter.requestDatas();
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void showError(String msg) {
        Toast.makeText(this,"showError",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void dismissLoading() {

    }

    @Override
    public void showResult(List<AppInfo> datas) {
        Toast.makeText(this,datas.get(0).getDisplayName(),Toast.LENGTH_SHORT).show();
    }

    @Override
    public void showNodata() {
        Toast.makeText(this,"showNodata",Toast.LENGTH_SHORT).show();
    }
}











  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值