网络层如何解耦问题

前言

Android业务层就是涉及app业务相关的,网络层为啥要触耦,因为一个好的架构和项目往往要适应不同开发场景和业务需求,如何有一天你的小同事需要替换网络库,用volley怎么办,那么本篇就是讲的如何解耦,其实大多还是用到观察者模式。

先看看老的代码截图如下:

public class HupuHttpHandler extends AsyncHttpResponseHandler {

	public BaseActivity a;
	public int messageID1;
	public int messageID2;

	public HupuHttpHandler(BaseActivity act) {
		a = act;
	}

	@Override
	public void onFinish() {
		super.onFinish();
	}

	@Override
	public void onSuccess(HttpEntity content, int reqType) {
		super.onSuccess(content, reqType);
		if (a != null && !a.isFinishing()) {
			BaseEntity o = JsonPaserFactory.paserObj(content, reqType);
			if (o != null && o.err != null) {
				a.onErrResponse(null, o.err, reqType);
				return;
			}
		
			if (messageID1 != 0)
				a.onReqResponse(o, reqType, messageID1, messageID2);
			else
				a.onReqResponse(o, reqType);
		}
	}

	@Override
	public void onFailure(Throwable error, String content, int reqType) {
		super.onFailure(error, content, reqType);
		if (a != null && !a.isFinishing()) {
			a.onErrResponse(error, content, reqType);
		}
	}

}

以上就是老的框架模型,大家看到a是代表了一个BaseActivity对象。这样就把业务层和网络库死死的绑在一块,如果采用面向接口模式的话,这肯定不符合,也不符合模块独立化的要求如下图:

        

/**
 * 请求callback;
 * @author hushaoping
 *
 */
public interface HttpCallback {
	/**
	 * 请求成功
	 * @param content   返回值 
	 * @param object    返回的转化对象
	 * @param reqType   请求的唯一识别
	 */
	public void onSuccess(String content,Object object,int reqType);
	
	/**
	 * 请求失败
	 * @param error    错误
	 * @param content   返回值
	 * @param reqType  请求的唯一识别
	 */
	public void onFailure(Throwable error, String content, int reqType);
}

       

public class BaseHttpHandler extends AsyncHttpResponseHandler {

	private HttpCallback response;
	public int messageID1;
	public int messageID2;

	public BaseHttpHandler(HttpCallback response) {
		this.response = response;
	}

	@Override
	public void onFinish() {
		super.onFinish();
	}

	@Override
	public void onSuccess(HttpEntity content, int reqType) {
		super.onSuccess(content, reqType);
		if (response!= null) {
			try {
				Log.i("HU","handle==content="+content);
				BaseEntity entity=JsonPaserFactory.paserObj(content, reqType);
				response.onSuccess(EntityUtils.toString(content), entity, reqType);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onFailure(Throwable error, String content, int reqType) {
		super.onFailure(error, content, reqType);
		if (response != null) {
			response.onFailure(error, content, reqType);
		}
	}

	
}

图上两张就能看到我们把BaseActivity中的a替换成了接口HttpCallback,这样就把业务层和网络层关系抽离出来 了,不能直接跟activity打交代,那么优化到这个地步就完了吗?肯定不是,如果要能随意切换网络库里,我们就才走出了第一步。如何实现多个网络同用,那就要考虑到做一个共性,设计模式中有装饰器模式,我个人理解就是装饰器模式就是一个商场,可以用来购物、吃饭、游玩等,至于具体解释请参考:

     http://blog.csdn.net/hguisu/article/details/7531960。

     那么这下应该找到一个共性,就是申请网络,大多用到get、post、参数传递、url、头部信息等。具体改造如下图:

/**
 * 请求对象模板接口
 * @author hushaoping
 *
 */
public interface BaseHttpClient {
	
	public void post(int reqType,Context context, String url, RequestParams params,
			HttpCallback callback);
	
	public void post(int reqType,Context context, String url, HttpEntity entity,
			String contentType, HttpCallback callback);
	
	public void post(int reqType,Context context, String url,
			Map<String, String> clientHeaderMap, RequestParams params,
			String contentType, HttpCallback callback);
	
	public void post(int reqType,Context context, String url, Header[] headers,
			HttpEntity entity, String contentType,
			HttpCallback callback);
	
	public void get(int reqType,String url, RequestParams cacheParams,
			HttpCallback callback);
	
	public void get(int reqType,Context context, String url, RequestParams cacheParams,
			HttpCallback callback);
	
}
/**
 * AsyncHttpClient async网络申请实现类
 * 如果有新网络tcp请求,就要重新实现一个网络交互类
 * 
 * @author 胡少平
 * 
 */
public class AsyncHttpClientImpl implements BaseHttpClient {
	
	private  AsyncHttpClient client=null;
	
	//单例模式实现
	private static AsyncHttpClientImpl instance;

	public static AsyncHttpClientImpl getHupuHttpClient() {
		if (instance == null)
			instance = new AsyncHttpClientImpl();
		return instance;
	}
	
	private AsyncHttpClientImpl() {
		client = new AsyncHttpClient();
	}

	@Override
	public void get(int reqType, String url, RequestParams cacheParams,
			HttpCallback callback) {
		client.get(url,cacheParams, new BaseHttpHandler(callback), reqType);
	}

	@Override
	public void get(int reqType, Context context, String url,
			RequestParams cacheParams, HttpCallback callback) {
		// TODO Auto-generated method stub
		client.get(url, cacheParams,new BaseHttpHandler(callback), reqType);
	}

	@Override
	public void post(int reqType, Context context, String url,
			RequestParams params, HttpCallback callback) {
		// TODO Auto-generated method stub
		client.post(url,params, new BaseHttpHandler(callback), reqType);
	}

	@Override
	public void post(int reqType, Context context, String url,
			HttpEntity entity, String contentType, HttpCallback callback) {
		// TODO Auto-generated method stub
		client.post(context,url, entity,contentType,new BaseHttpHandler(callback), reqType);
	}

	@Override
	public void post(int reqType, Context context, String url,
			Map<String, String> clientHeaderMap, RequestParams params,
			String contentType, HttpCallback callback) {
		// TODO Auto-generated method stub
	}

	@Override
	public void post(int reqType, Context context, String url,
			Header[] headers, HttpEntity entity, String contentType,
			HttpCallback callback) {
		// TODO Auto-generated method stub
		client.post(context,url, headers,entity,contentType,new BaseHttpHandler(callback), reqType);
	}
	
}



你可能感兴趣的:(框架,观察者模式,网络,Android架构)