隨筆 - 0, 文章 - 11, 評論 - 0, 引用 - 0
          數據加載中……

          責任鏈模式 簡單模擬 Interceptor 攔截器

          結構圖:

          Intercept Interface :
          public interface Intercept {
              
          public void intercept (ActionInvocation invocation) ;
          }

          First Intercept Implement:
          public class FirstIntercept implements Intercept {

              
          public void intercept(ActionInvocation invocation) {
                  System.out.println(
          "begin First Intercept to do somethin");
                  
          try {
                      invocation.invoke() ;
                  } 
          finally {}
                  System.out.println(
          "end First Intercept to do somethin");
              }

          }

          Second Intercept Implement :
          public class SecondIntercept implements Intercept {

              
          public void intercept(ActionInvocation invocation) {
                  System.out.println(
          "begin Second Intercept to do somethin");
                  
          try {
                      invocation.invoke() ;
                  } 
          finally {}
                  System.out.println(
          "end Second Intercept to do somethin");
              }

          }

          Third InterceptImplement :
          public class ThirdIntercept implements Intercept {

              
          public void intercept(ActionInvocation invocation) {
                  System.out.println(
          "begin Third Intercept to do somethin");
                  
          try {
                      invocation.invoke() ;
                  } 
          finally {}
                  System.out.println(
          "end Third Intercept to do somethin");
              }

          }

          ActionInvocation :
          import java.util.ArrayList;
          import java.util.List;

          public class ActionInvocation {
              
          private static int index = 0 ;
              
          private List<Intercept> intercepts = new ArrayList<Intercept> ();
              
              
          /*
               * 這里用構造加載攔截器
               
          */
              
          public ActionInvocation () {
                  intercepts.add(
          new FirstIntercept()) ;
                  intercepts.add(
          new SecondIntercept()) ;
                  intercepts.add(
          new ThirdIntercept()) ;
              }

              
          public void invoke () {
                  
          if (index < intercepts.size()) {
                      intercepts.get(index
          ++).intercept(this); ;
                  }
              }
          }

          執行結果:
          begin First Intercept to do somethin
          begin Second Intercept to do somethin
          begin Third Intercept to do somethin
          end Third Intercept to do somethin
          end Second Intercept to do somethin
          end First Intercept to do somethin

          流程圖:

          posted on 2014-05-14 17:32 00001000 閱讀(362) 評論(0)  編輯  收藏 所屬分類: 設計模式


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 依安县| 蒙自县| 玉溪市| 林芝县| 威信县| 全椒县| 白沙| 永城市| 长沙市| 灵宝市| 怀安县| 朔州市| 武陟县| 桓仁| 杭锦旗| 冕宁县| 白玉县| 乐东| 勐海县| 射洪县| 东莞市| 五莲县| 江陵县| 旌德县| 兴山县| 长治县| 隆子县| 三明市| 高尔夫| 银川市| 绥宁县| 巴里| 古浪县| 梁山县| 昂仁县| 上栗县| 宜宾县| 旬阳县| 安泽县| 济阳县| 日照市|