实现拦截器

Posted on 2005-12-28 19:02 李岚 阅读(548) 评论(0)  编辑  收藏 所属分类: Java

背景:

拦截器是现在很多程序必须的一个东西。比如要在某个方法的前后做些处理,在一些特定的情况下可能会导致大量的重复代码。而假如我们拥有了类似拦截器一样的东西,那我们就可以任意在自己希望的方法的执行前后做我们自己的处理,比如日志,取得参数或者是事务等。

 

实现:

1.首先定义一个业务接口

 1/**
 3 * 一个业务对象接口,所有的业务操作都应该在execute方法中去执行
 7 * @author Administrator
11 */

12
13public interface Action 
{
14

15    public void execute();

21}

22

 

2         定义一个拦截器接口

 

 1/**
 2
 3 * 拦截器接口,可以在方法前后执行操作

 7 * @author Administrator
11 */

13public interface Interceptor {
16

17    
/**
19     * 初始化方法,根据需要实现
22
23     */

24
25    void init();

28
29    
/**
30
31     * 终结方法,根据需要实现

35     */

36
37    void destory();

40
41    
/**
42
43     * 拦截器的具体实现

47     * @param invocation  一个Action 的包装对象,以后修改为能指定任意对象
49     * @return 一个状态
51     * @throws Exception
53     */

55    String intercept(ActionInvocation invocation) throws Exception;
57}

58

 

3         实现一个默认的拦截器

 

 1/**
 2
 3 * 拦截器接口,可以在方法前后执行操作

 7 * @author Administrator
11 */

12
13public interface Interceptor 
{
17    /**
19     * 初始化方法,根据需要实现
23     */

25    void init();
29    /**
31     * 终结方法,根据需要实现
35     */

36
37    void destory();

41    /**
43     * 拦截器的具体实现
47     * @param invocation  一个Action 的包装对象,以后修改为能指定任意对象
49     * @return 一个状态
51     * @throws Exception
53     */

54
55    String intercept(ActionInvocation invocation) throws
 Exception;
56

57}

58

 

4         实现一个Action接口的包装器,这里实现了拦截器的核心方法

  1/**
  2
  3 * 默认实现

  5 * @author Administrator
  9 */

 10
 11public class DefaultActionInvocation implements ActionInvocation 
{
 12

 13       private Object action;

 17       private Iterator  interceptors;
 21       DefaultActionInvocation(Object action, List interceptors) {
 23              this.action = action;
 27              this.interceptors = interceptors.iterator();
 29       }


 33       public Object getAction() {
 35              return action;
 37       }

 38
 39
 
 40

 41       public void setAction(Object action) 
{
 43              this.action = action;
 45       }

 49       public Iterator getInterceptors() {
 51              return interceptors;
 53       }

 57       public void setInterceptors(Iterator interceptors) {
 59              this.interceptors = interceptors;
 61       }


 67       /**
 69        * 调用拦截器
 71        */

 72
 73       public String invoke() throws Exception 
{
 74

 75              if (interceptors.hasNext()) 
{
 77                     Interceptor interceptor = (Interceptor) interceptors.next();
 79                     interceptor.intercept(this);
 81              }
 else {
 83                     this.invokeActionOnly();
 85              }

 89              return null;
 91       }

 92
 93
 
 94

 95       public String invokeActionOnly() throws Exception 
{
 97              return invokeAction(getAction());
 99       }

100
101    
/**
103     * 调用Action的execute
105     * @param action
107     * @return
109     */

111       private String invokeAction(Object action) {
113              if (action instanceof Action) {
115                     Action instance = (Action) action;
117                     instance.execute();
119              }

121              return null;
123       }

127}

128
129

 

5         如何使用?

 

 1/**
 2
 3 * 定义的一个业务对象

 7 * @author Administrator
11 */

12
13public class MyAction implements Action 
{
14

15       
/**
17        * 执行一个业务操作,这里是print操作
19        */

20
21       public void execute() 
{
23              System.out.println("执行业务逻辑…");
25       }


31       /**
32
33        * client调用的方式

35        * @param args
37        * @throws Exception
39        */

40
41       public static void main(String[] args) throws Exception 
{
42

43              Action a = new
 MyAction();
44

45              // 定义2个拦截器

46
47              Interceptor i1 = new L1();   //继承了AbstractInterceptor的一个拦截器
49              Interceptor i2 = new L2();   //继承了AbstractInterceptor的一个拦截器
51              // Interceptor i3 = new L2();
52
53              List interceptors = new ArrayList();
54

55              interceptors.add(i1);

57              interceptors.add(i2);
58

59              // 设置拦截器

61              ActionInvocation myaction = new DefaultActionInvocation(a, interceptors);
62

63              //执行

65              myaction.invoke();
66

67       }

71}

72
73

 

可以改进的地方:

        1)  可以使用动态代理,这样就可以对任意对象使用拦截器了。而不仅仅是Action接口

        2)  拦截器可以通过一个配置文件读取,比如一个XML文件,而不是现在这样写死在客户端代码中。Digester给我们提供了解析XML的帮助
3)可以使用
Commons Collections 里的ResettableIterator,这样每次Iterator就可以reset了,不然我们只有每次都使用新的ActionInvocation


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 7, comments - 23, trackbacks - 0, articles - 0

Copyright © 李岚