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

          攔截器的實現原理

          運行主類
          Main.java

          public class Main {
              public static void main(String[] args) {
                  new ActionInterceptor().invoke() ;
              }
          }

          /********************************/

          攔截器核心
          ActionInterceptor.java

          import java.util.ArrayList;
          import java.util.List;

          public class ActionInterceptor {
              List<Inte> intes = new ArrayList<Inte>() ;
              int index = -1 ;
              public ActionInterceptor() {
                  intes.add(new FistInte()) ;
                  intes.add(new SecondInte()) ;
              }
              public void invoke() {
                  index++ ;
                  if(index >= intes.size()) {
                      new Action().execute() ;
                  }else {
                      this.intes.get(index).inte(this) ;
                  }
              }
          }

          /********************************/

          接口:與攔截器之間的通訊接口
          Inte.java

          public interface Inte {
              void inte(ActionInterceptor actioninterceptor) ;
          }

          /********************************/

          接口使用類
          FistInte.java

          public class FistInte implements Inte {

              @Override
              public void inte(ActionInterceptor actioninterceptor) {
                  System.out.println(1) ;
                  actioninterceptor.invoke() ;
                  System.out.println(-1) ;
              }
          }

          SecondInte.java
          public class SecondInte implements Inte {

              @Override
              public void inte(ActionInterceptor actioninterceptor) {
                  System.out.println(2) ;
                  actioninterceptor.invoke() ;
                  System.out.println(-2) ;
              }
          }

          /********************************/

          動作類

          Action.java
          public class Action {
              public void execute() {
                  System.out.println("execute!") ;
              }
          }

          posted on 2010-07-31 15:19 jack zhai 閱讀(500) 評論(0)  編輯  收藏 所屬分類: Struts2

          主站蜘蛛池模板: 尉氏县| 荔波县| 潜江市| 武隆县| 永泰县| 宣化县| 上犹县| 高要市| 黑水县| 东兴市| 远安县| 芜湖县| 新建县| 托里县| 江川县| 武胜县| 宁国市| 五台县| 永寿县| 江西省| 揭东县| 塔河县| 安多县| 庆安县| 高安市| 威远县| 桦川县| 阳东县| 天全县| 青铜峡市| 德州市| 西充县| 绥阳县| 个旧市| 九龙县| 澄迈县| 石狮市| 龙川县| 宁都县| 乌苏市| 宜良县|