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

          攔截器的實(shí)現(xiàn)原理

          運(yùn)行主類
          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) ;
              }
          }

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

          動(dòng)作類

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

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

          主站蜘蛛池模板: 资阳市| 固镇县| 伊吾县| 鹤壁市| 桑植县| 茶陵县| 伊金霍洛旗| 建宁县| 邛崃市| 新绛县| 洛宁县| 瓮安县| 宁河县| 大兴区| 承德县| 体育| 土默特左旗| 分宜县| 来安县| 大余县| 屏南县| 广昌县| 虹口区| 蕉岭县| 原阳县| 河曲县| 淳化县| 确山县| 中牟县| 招远市| 杭锦后旗| 庄河市| 阳东县| 阜南县| 安岳县| 嘉善县| 佛冈县| 兰州市| 镇赉县| 万载县| 潮州市|