1IC层
package com.example.administrator.ykmn.ui; public interface IContact { public interface IView{ void showData(String message); } public interface IPresent{ void attachView(IView iView); void detachView(IView iView); void resquestModel(); } public interface IModel{ interface onCallBack{ void onCallBack(String message); } //接口 void requestData( onCallBack onCallBack); } }
2IP层
package com.example.administrator.ykmn.ui; import java.lang.ref.WeakReference; public class IPresentlmp implements IContact.IPresent { IContact.IView iView; private IContact.IModel modelmp; private WeakReference<IContact.IView> viewWeakReference; private WeakReference<IContact.IModel> modelWeakReference; @Override public void attachView(IContact.IView iView) { this.iView=iView; modelmp = new IModelmp(); viewWeakReference = new WeakReference<>(iView); modelWeakReference = new WeakReference<>(modelmp); } @Override public void detachView(IContact.IView iView) { viewWeakReference.clear(); modelWeakReference.clear(); } @Override public void resquestModel() { modelmp.requestData(new IContact.IModel.onCallBack() { @Override public void onCallBack(String message) { iView.showData(message); } }); } }
3IM层
package com.example.administrator.ykmn.ui; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; public class IModelmp implements IContact.IModel { public static final String url = "http://www.zhaoapi.cn/product/getCarts?uid=71"; @Override public void requestData( final onCallBack onCallBack) { OkHttpClient okHttpClient = new OkHttpClient.Builder().build(); Call call = okHttpClient.newCall(new Request.Builder().url(url).build()); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { String responseString = response.body().string(); onCallBack.onCallBack(responseString); } }); } }