责任链模式应用

来源:互联网 发布:网络预约车管理办法 编辑:程序博客网 时间:2024/06/05 16:04


原创帖子,转载请注明出处:http://blog.csdn.net/sbvfhp/article/details/48227601


这里主要应用的单例设计模式和责任链设计模式(责任链设计模式参看如下链接)
http://blog.csdn.net/sbvfhp/article/details/48227309



项目结构如下:



定义超类:

package com.http.linkmode;public abstract class HttpHandler{  public HttpHandler mHttpHandler;  public abstract void handleHttp();  public HttpHandler setNextHandler(HttpHandler pHttpHandler)  {    mHttpHandler = pHttpHandler;    return mHttpHandler;  }}


定义第一步子任务:

package com.http.linkmode;import android.util.Log;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import httpcor.yzsd.com.httpcor.BaseApplication;/* * 2015-09-05 * 定义第一步子任务 */public class Step1HttpHandler extends HttpHandler{  private volatile static Step1HttpHandler singleton;  public static Step1HttpHandler getSingleton() {    if (singleton == null) {      synchronized (Step1HttpHandler.class) {        if (singleton == null) {          singleton = new Step1HttpHandler();        }      }    }    return singleton;  }  @Override  public void handleHttp()  {    System.out.println("Step1HttpHandler Start");    StringRequest stringRequest = new StringRequest(UrlManage.URL_WEATHER,        new Response.Listener<String>() {          @Override          public void onResponse(String response) {            //这里将每一步请求的返回结果以键值对的形式存入LinkedHashMap            BaseApplication.mListHttpHandler.put(System.currentTimeMillis() + "Step1HttpHandler",                response.substring(0, 10));            BaseApplication.getLinkHttpHandler();            //这里将根据请求返回的结果,进行下一步的路由            httpRouter("Step2HttpHandler");          }        }, new Response.ErrorListener() {          @Override          public void onErrorResponse(VolleyError error) {            Log.e("TAG", error.getMessage(), error);          }        });    BaseApplication.getRequestQueue().add(stringRequest);    System.out.println("Step1HttpHandler End");  }  public void httpRouter(String routerTag) {    switch (routerTag) {      case "Step1HttpHandler":        mHttpHandler = Step1HttpHandler.getSingleton();//在这里获取下一步链节点的单例        mHttpHandler.handleHttp();        break;      case "Step2HttpHandler":        mHttpHandler = Step2HttpHandler.getSingleton();        mHttpHandler.handleHttp();        break;      default:        break;    }  }}


定义第二步子任务:

package com.http.linkmode;import android.util.Log;import com.android.volley.Response;import com.android.volley.VolleyError;import com.android.volley.toolbox.StringRequest;import httpcor.yzsd.com.httpcor.BaseApplication;/* * 2015-09-05 * 定义第二子任务 */public class Step2HttpHandler extends HttpHandler{  private volatile static Step2HttpHandler singleton;  public static Step2HttpHandler getSingleton() {    if (singleton == null) {      synchronized (Step2HttpHandler.class) {        if (singleton == null) {          singleton = new Step2HttpHandler();        }      }    }    return singleton;  }  @Override  public void handleHttp()  {    System.out.println("Step2HttpHandler Start");    StringRequest stringRequest = new StringRequest(UrlManage.URL_WEATHER,            new Response.Listener<String>() {              @Override              public void onResponse(String response) {                //这里将每一步请求的返回结果以键值对的形式存入LinkedHashMap                BaseApplication.mListHttpHandler.put(System.currentTimeMillis() + "Step2HttpHandler",                        response.substring(0, 10));                BaseApplication.getLinkHttpHandler();                //这里将根据请求返回的结果,进行下一步的路由                httpRouter("Step3HttpHandler");              }            }, new Response.ErrorListener() {      @Override      public void onErrorResponse(VolleyError error) {        Log.e("TAG", error.getMessage(), error);      }    });    BaseApplication.getRequestQueue().add(stringRequest);    System.out.println("Step2HttpHandler End");  }  public void httpRouter(String routerTag) {    switch (routerTag) {      case "Step3HttpHandler":        mHttpHandler = Step3HttpHandler.getSingleton();//在这里获取下一步链节点的单例        mHttpHandler.handleHttp();        break;      case "Step4HttpHandler":        mHttpHandler = Step4HttpHandler.getSingleton();        mHttpHandler.handleHttp();        break;      default:        break;    }  }}



定义第三步子任务




定义第四步子任务










0 0