1.model层
public class ShopCartModel { private IPresenter iPresenter; public ShopCartModel(IPresenter iPresenter){ this.iPresenter=iPresenter; } public void getShopData(String uid){ Map<String,String> map=new HashMap<>(); map.put("uid",uid); RetrofitUtil.getInstance().getApiService() .getNews(map) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(new DisposableSubscriber<ShopCarBean>() { @Override public void onNext(ShopCarBean shopCarBean) { iPresenter.onSuccess(shopCarBean); } @Override public void onError(Throwable t) { iPresenter.onFiled(t); } @Override public void onComplete() { } }); } }
2.presenter层
public class ShopCarPresenter implements IPresenter<ShopCarBean> { private IView iView; public ShopCarPresenter(IView iView){ this.iView=iView; } public void getShopData(String uid){ ShopCartModel shopCartModel = new ShopCartModel(this); shopCartModel.getShopData(uid); } @Override public void onSuccess(ShopCarBean shopCarBean) { iView.onSuccess(shopCarBean); } @Override public void onFiled(Throwable throwable) { iView.onFiled(throwable); } public void detachView() { if (iView != null) { iView=null; } } }
接口
public interface IPresenter<T> { void onSuccess(T t); void onFiled(Throwable throwable); }
public interface IView<T> { void onSuccess(T t); void onFiled(Throwable throwable); }