隨筆 - 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)  編輯  收藏 所屬分類: 設計模式


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


          網站導航:
           
          主站蜘蛛池模板: 潜山县| 沽源县| 常宁市| 丹东市| 武冈市| 新化县| 高雄市| 镇平县| 安塞县| 灵石县| 长岭县| 松原市| 嵊州市| 上思县| 汶川县| 乃东县| 合阳县| 佛坪县| 长宁县| 郑州市| 全南县| 崇阳县| 西丰县| 巴林右旗| 凉城县| 西宁市| 格尔木市| 江油市| 长治市| 崇信县| 义马市| 三江| 上饶市| 青神县| 赤峰市| 荥阳市| 石门县| 蒲城县| 子长县| 和政县| 克什克腾旗|