最近在做手机rom OTA项目,项目不算大但原来的代码耦合比较高,对后期维护不是很好,,设计的好处不用多说增强代码复用、更容易扩展、更容易读懂、更容易维护,索性大刀阔斧逐步进行代码重构、优化,其中对volley的使用也是问题比较多,所以优先重构了下,后续将逐步对各个不合理的地方重构,后面每个项目都会规划思维导图、类图进行设计不在做码农,具体代码如附件,整体设计类图如下(当然画类图的水平还有待提升):
重构前代码:
public void startCheckUpdateTask() {
// If it is checking for updates, then not allowed to check again.
if (isCheckingUpdate)
return;
// Start checking for updates
isCheckingUpdate = true;
// Get the actual ROM Update Server URL
URI updateServerUri = RequestBuilder.CHECK_UPDATE_VERSION4_URI_JSON;
UpdatesJsonObjectRequest request;
try {
request = new UpdatesJsonObjectRequest(updateServerUri.toASCIIString(), Utils.getUserAgentString(this), CommonUtil.buildUpdateRequest(this), this, this);
// Set the tag for the request, reuse logging tag
request.setTag(TAG);
} catch (JSONException e) {
Log.e(TAG, "Could not build request", e);
return;
}
((OTApplication) getApplicationContext()).getQueue().cancelAll(TAG);
((OTApplication) getApplicationContext()).getQueue().getCache().clear();
((OTApplication) getApplicationContext()).getQueue().add(request);
}
重构后代码:
ApiContext.getDefault().getUpdateRequest(this, new ICallback<EntireResponse>() {
@Override
public void onSuccess(EntireResponse obj) {
}
@Override
public void onError(String error) {
}
});
代码下载地址:http://download.csdn.net/download/junjianzhang/9283273