[转]NSProxy实现AOP方便为ios应用实现异常处理策略
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了[转]NSProxy实现AOP方便为ios应用实现异常处理策略,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5412字,纯文字阅读大概需要8分钟。
内容图文
[转载自:http://blog.csdn.net/yanghua_kobe/article/details/8395535]
前段时间关注过objc实现的AOP,在GitHub找到了其中的两个库:AOP-in-Objective-C 和 AOP-for-Objective-C。第一个是基于NSProxy来实现的;第二个是基于GCD以及block实现的。两者都使用了Cocoa的运行时编程技术,将拦截器注入给代理对象,使其干涉真是对象的执行顺序从而达到给代码增加“切面”的目的,这里的模式就是通常的代理模式。
因为时间关系,暂时只看了第一个库的代码,下面简短地分析一下。
NSProxy:如其名,它出现的目的就是为了来“代理”一个真实对象的。这是Foundation库的内置实现。大部门人都知道NSObject是通常Cocoa中的根类,没错,但其实根类并不止一个,NSProxy也是跟NSObject的根类,只是它是个抽象类并且不是用于通常意义上的编程目的,所以不是那么广为人知(事实上我也是今天才知道)。并且NSObject看到它你以为它是个类。但今天看NSProxy定义的时候,我发现它的头文件里是这样定义的:
1 @interface NSProxy <NSObject>
开始我很莫名其妙,如果是继承自NSObject的话,应该是个冒号。这种写法明显就是实现协议的写法啊。于是,查看了一下资料,果然还有个NSObject的协议,并且NSObject类自身也实现了NSObject协议。具体资料请看
这篇文章
。
NSProxy与NSObject一虚一实,并且它们都实现了NSObject协议。这让NSProxy的实现类能够很好地“代理”NSObject子类,并且把NSObject协议抽象出来,也让他们能够共享某些行为。来看看它是如何工作的(测试代码见AOPLibTest.m文件):
在你需要使用AOP的地方,你首先需要实例化一个对象,比如你需要实例化一个NSMutableArray,你需要使用AOPProxy来实例化它:
1 NSMutableArray* testArray = (NSMutableArray*)[[AOPProxy alloc] initWithNewInstanceOfClass:[NSMutableArray class]];
这里,其实是间接实例化。它提供了一个接口,你把你的类名传给它,由它给你实例化。事实上,这是一种注入方式,而看完这个方法的定义你就会看到其实它返回给你的并不是NSMutableArray的一个实例(其实是AOPProxy,而 它们之所以能互相强制转换是因为他们都实现了NSObject协议 ):
1 - (id) initWithNewInstanceOfClass:(Class) class { 2 3// create a new instance of the specified class 4id newInstance = [[class alloc] init]; 5 6// invoke my designated initializer 7 [self initWithInstance:newInstance]; 8 9// release the new instance 10 [newInstance release]; 1112// finally return the configured self 13return self; 14 }
上面的self指代的就是AOPProxy,其中的initWithInstance方法:
1 - (id) initWithInstance:(id)anObject { 23 parentObject = [anObject retain]; 45 methodStartInterceptors = [[NSMutableArray alloc] init]; 6 methodEndInterceptors = [[NSMutableArray alloc] init]; 78return self; 9 }
可以看到,它在内部hold住了真实对象,并且实例化了两个数组,用来存储方法执行前后的拦截器集合。
下面,我们可以为NSMutableArray增加拦截器了:
1 [(AOPProxy*)testArray interceptMethodStartForSelector:@selector(addObject:) 2 withInterceptorTarget:self 3 interceptorSelector:@selector( addInterceptor: )]; 45 [(AOPProxy*)testArray interceptMethodEndForSelector:@selector(removeObjectAtIndex:) 6 withInterceptorTarget:self 7 interceptorSelector:@selector( removeInterceptor: )];
因为这两个方法是AOPProxy的实例方法,所以在编写的时候还是需要在强制转回来(其实你在XCode里跟踪的时候,这里的testArray一直都是APOProxy类型的对象,因为一开始他就是被AOPPorxy allo出来的)。这两个方法的实现很简单,只是将拦截器假如相应的数组中去,待后面取出来执行。
1 [testArray addObject:[NSNumber numberWithInt:1]]; 2 [testArray removeObjectAtIndex:0];
好了,看起来这里开始调用某个对象本身的行为了。为什么说看起来呢?难道不是吗。当然不是,我在上面已经说过了,这里只是取名为testArray事实上它并不是NSMutableArray的实例,而是AOPProxy的实例。但为什么它还是可以调用addObject这个方法呢,因为它被强制转换为NSMutableArray类型了,编辑器能够接受这样的类型转换,也就是这是合法的。所以编辑器认为它就是NSMutableArray类型的对象了,所以是可以这么调用的,但后面你会看到。在运行时其实编译器知道了它不是真实的NSMutableArray类型(也就是说它无法响应addObject以及removeObjectAtIndex这两个方法),所以把它交给了另一个专门的方法来处理这些无法响应的消息:
- ( void ) forwardInvocation: ( NSInvocation * ) anInvocation;
这个方法其实是继承自NSPorxy,NSProxy对它的实现其实就是抛出个异常,子类需要重新实现它,把它消息传递给真实的对象。详细信息参考官方文档!
来看看它的实现:
1 - (void)forwardInvocation:(NSInvocation *)anInvocation; 2{ 3 SEL aSelector = [anInvocation selector]; 4 5// check if the parent object responds to the selector ... 6if ( [parentObject respondsToSelector:aSelector] ) { 7 8 [anInvocation setTarget:parentObject]; 910//11// Intercept the start of the method. 12//1314 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 1516for ( int i = 0; i < [methodStartInterceptors count]; i++ ) { 1718// first search for this selector ... 19 AOPInterceptorInfo *oneInfo = [methodStartInterceptors objectAtIndex:i]; 2021if ( [oneInfo interceptedSelector] == aSelector ) { 2223// extract the interceptor info 24id target = [oneInfo interceptorTarget]; 25 SEL selector = [oneInfo interceptorSelector]; 2627// finally invoke the interceptor 28 [(NSObject *) target performSelector:selector withObject:anInvocation]; 29 } 30 } 3132 [pool release]; 3334//35// Invoke the original method ... 36//3738 [self invokeOriginalMethod:anInvocation]; 394041//42// Intercept the ending of the method. 43//4445 NSAutoreleasePool *pool2 = [[NSAutoreleasePool alloc] init]; 4647for ( int i = 0; i < [methodEndInterceptors count]; i++ ) { 4849// first search for this selector ... 50 AOPInterceptorInfo *oneInfo = [methodEndInterceptors objectAtIndex:i]; 5152if ( [oneInfo interceptedSelector] == aSelector ) { 5354// extract the interceptor info 55id target = [oneInfo interceptorTarget]; 56 SEL selector = [oneInfo interceptorSelector]; 5758// finally invoke the interceptor 59 [(NSObject *) target performSelector:selector withObject:anInvocation]; 60 } 61 } 6263 [pool2 release]; 64 } 65// else { 66// [super forwardInvocation:invocation]; 67// } 68 }
可以看到这里让真实的对象调用了方法,并且干涉了对象的行为,在其前后加入了拦截器的执行操作。从而“优雅”地实现了AOP。
该库中,还提供了两个Aspect:
AOPMethodLoger-用于简单记录方法的日志;
AOPThreadInvoker-用于在一个单独的线程上执行方法;
之前在Java以及.net中已经很广泛地应用了AOP的实例了,常见的应用有做Log啊,异常捕获啊之类的。最近在做iOS的应用,其中也会牵扯到异常捕获的问题,特别是牵扯到数据库操作以及业务逻辑上的异常,总是写代码捕获块儿,费事还占面积。所以,我在里面又加了一个Aspect:AOPExcettionCatcher。很简单,就是在这里统一实现了异常捕获。
重新实现了invokeOriginalMethod方法:
1 - (void)invokeOriginalMethod:(NSInvocation *)anInvocation{ 2 NSLog(@"%@",@"entry into try block"); 3@try { 4 [super invokeOriginalMethod:anInvocation]; 5 } 6@catch (NSException *exception) { 7 NSLog(@"%@",@"entry into catch block"); 8 NSLog(@"%@",[exception reason]); 9 } 10@finally { 11 NSLog(@"%@",@"entry into finally block"); 12 } 13 }
当然了这只是应用之一,你还可以用它做更多的事情。
原文:http://www.cnblogs.com/crazypebble/p/3594527.html
内容总结
以上是互联网集市为您收集整理的[转]NSProxy实现AOP方便为ios应用实现异常处理策略全部内容,希望文章能够帮你解决[转]NSProxy实现AOP方便为ios应用实现异常处理策略所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。