fabrication的拦截器Interceptors简介

简介: 简介: Interceptors(拦截器),主要目的是为了改变PureMVC的消息通知在到达Commands和Mediators的正常执行顺序。 在拦截器里可以: ·废弃notification不再向外广播 ·修改notificationg再向外广播 ·使用新的notification替换原有的no...

简介:

Interceptors(拦截器),主要目的是为了改变PureMVC的消息通知在到达Commands和Mediators的正常执行顺序。 在拦截器里可以:

·废弃notification不再向外广播

·修改notificationg再向外广播

·使用新的notification替换原有的notification

·无限制发送这一次notification

·Interceptors与commands类似,可以使用PureMVC实例访问和修改应用程序

 

依赖:

Fabrication V0.6+ 可使用此功能

 

Interceptors的注册

Interceptors使用registerInterceptor方法来进行的注册,该方法可在FabricationFacade或是SimpleFabricationCommand的子类中使用。

语法,registerInterceptor(noteName:String, clazz:Class, parameters:Object=null)

noteName:注册notification它的名称

clazz:拦截器的处理类

parameters:可选参数,供拦截器处理类使用

// registers the interceptor for the save notification name.
registerInterceptor("save", MyInterceptor);

// registers the interceptor with save notification with extra parameters
registerInterceptor("save", MyInterceptor, {foo:"bar"});
 
Interceptors的实现
拦截类的处理类(前面提及的clazz),必须继承自AbstractInterceptor类,并实现intercept方法。
当注册的Notifications被发送时(使用sendNotification或是routerNotification方法进行发送),
拦截器会调用该处理类的intercept方法,这些方法中可以直接使用的对象有:
notification:被拦截的notification对象
parameters:拦截器注册时传入的可选参数
processor:对外提供的几种方法,忽略、跳过或中止该notification的广播,包含三个方法<proceed|abort|skip>
 
proceed:允许notification继续进行广播,可以使用新的notification替换当前的notification。
skip:如果所有对该notification的拦截器处理已经完成,就直接调用完成方法。不然就再调用该notification的其它拦截器实例
abort:直接中止该notification的广播,并立即调用完成方法。
 
在框架源码中可以看到上述三个方法中的实现:
   1: public function getNotification():INotification {
   2:     return notification;
   3: }
   4:  
   5: /**
   6:  * Adds a interceptor to the list of interceptors for the current notification.
   7:  * 
   8:  * @param interceptor The interceptor object to register.
   9:  */
  10: public function addInterceptor(interceptor:IInterceptor):void {
  11:     interceptor.processor = this;            
  12:     interceptors.push(interceptor);
  13: }
  14:  
  15: /**
  16:  * Removes the specified interceptor from the list of interceptors.
  17:  * 
  18:  * @param interceptor The interceptor object to remove.
  19:  */
  20: public function removeInterceptor(interceptor:IInterceptor):void {
  21:     var index:int = interceptors.indexOf(interceptor);
  22:     if (index >= 0) {
  23:         interceptors.splice(index, 1);
  24:         interceptor.dispose();
  25:     }
  26: }
  27:  
  28: /**
  29:  * Runs all interceptors registered with this NotificationProcessor.
  30:  */
  31: public function run():void {
  32:     var n:int = interceptors.length;
  33:     var interceptor:IInterceptor;
  34:     for (var i:int = 0; i < n; i++) {
  35:         interceptor = interceptors[i];
  36:         
  37:         interceptor.notification = notification;
  38:         interceptor.intercept();
  39:     }
  40: }
  41:  
  42: /**
  43:  * Sends a proceed event so that the notification can be send to the rest of
  44:  * the PureMVC actors. Flags this instance as complete to ignore other interceptors.
  45:  * Also sends a finish event to indicate that the processor can be disposed.
  46:  * 
  47:  * @param note The notification to proceed with. If null the current notification object is used.
  48:  */
  49: public function proceed(note:INotification = null):void {
  50:     if (!finished) {
  51:         dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.PROCEED, note));
  52:         finish();
  53:     }
  54: }
  55:  
  56: /**
  57:  * Sends an abort event. Flags this instance as complete to ignore other interceptors.
  58:  * Also sends a finish event to indicate that the processor can be disposed.
  59:  */
  60: public function abort():void {
  61:     if (!finished) {
  62:         dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.ABORT));
  63:         finish();
  64:     }
  65: }
  66:  
  67: /**
  68:  * If all interceptors have been skipped then sends a finish event to indicate
  69:  * that the processor can be disposed.
  70:  */
  71: public function skip():void {
  72:     if (!finished && ++skipCount == interceptors.length) {
  73:         finish();
  74:     }
  75: }
  76:  
  77: /**
  78:  * Flags the notification as finished and sends a finish event.
  79:  */
  80: public function finish():void {
  81:     finished = true;
  82:     dispatchEvent(new NotificationProcessorEvent(NotificationProcessorEvent.FINISH));
  83: }

interceptor的处理过程是异步的,实例中对”save”这条消息进行了监听响应,也对“save”进行了拦截处理,但只有在点击“继续”按钮的时候才继续广播该notification。

 

英文原文(能力有限,本文翻译的可能有误,欢迎批评和斧正):http://code.google.com/p/fabrication/wiki/Interceptors#Requirements

示例demo的源码:http://code.google.com/p/fabrication/source/browse/examples/interceptor_demo/src#src%2Fmain%2Fflex%2Fcontroller%253Fstate%253Dclosed

 

本地查看效果实例:

 

如需要本示例程序的完整代码,立即下载>>

目录
相关文章
|
4月前
|
Java 应用服务中间件 数据安全/隐私保护
面试官:过滤器Filter和拦截器Interceptors有什么区别?
面试官:过滤器Filter和拦截器Interceptors有什么区别?
31 0
|
7月前
|
前端开发 Java 数据库连接
【SpringMVC】JSR 303与interceptor拦截器快速入门
JSR 303是Java规范请求(Java Specification Request)的缩写,意思是Java 规范提案。是指向JCP(Java Community Process)提出新增一个标准化技术规范的正式请求。任何人都可以提交JSR,以向Java平台增添新的API和服务。JSR已成为Java界的一个重要标准。 JSR-303 是JAVA EE 6 中的一项子规范,叫做Bean Validation,Hibernate Validator 是 Bean Validation 的参考实现 . Hibe
|
8月前
|
监控 Java Spring
gRPC中interceptor拦截器的总结和实践
gRPC中的interceptor拦截器分为客户端拦截器和服务端拦截器,分别是在客户端和服务端的请求被发送出去之前进行处理的逻辑。常见的使用场景有:(1)请求日志记录及监控;(2)添加请求头数据、以便代理转发使用;(3)请求或者结果重写。
163 0
gRPC中interceptor拦截器的总结和实践
QGS
|
11月前
|
存储 调度 数据安全/隐私保护
入门SpringMVC之Interceptor拦截器
SpringMVC中的Interceptor拦截器,它的主要作用是拦截指定的用户需求,并进行相应的预处理与后处理。
QGS
66 0
|
前端开发 Java Spring
SpringMVC拦截器实现原理
SpringMVC拦截器实现原理
125 0
SpringMVC拦截器实现原理
|
存储 中间件 测试技术
gRPC(六)进阶:拦截器 interceptor
拦截器本质上就是一个特定类型的函数,所以实现拦截器只需要实现对应类型方法(方法签名相同)即可。
707 1
gRPC(六)进阶:拦截器 interceptor
|
前端开发
SpringMVC拦截器的基本使用
1.拦截器(interceptor)的作用 (1)SpringMVC的拦截器类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理。 (2)将拦截器按一定的顺序联结成一条链,这条链称为拦截器链(interceptor Chain)。在访问被拦截的方法或字段时,拦截器链中的拦截器就会按其之前定义的顺序被调用。拦截器也是AOP思想的具体体现。
|
开发者
拦截器|学习笔记
快速学习拦截器
|
设计模式 Java 数据安全/隐私保护
拦截器 | 学习笔记
快速学习拦截器,介绍了拦截器系统机制, 以及在实际应用过程中如何使用。
110 0
拦截器 | 学习笔记
|
前端开发
jfinal拦截器Interceptor解析
jfinal拦截器Interceptor解析
269 0
jfinal拦截器Interceptor解析