Objective-C 下的 AOP 編程,objective-caop
Objective-C 下的 AOP 編程概念
在軟體業,AOP為Aspect Oriented Programming的縮寫,意為:面向切面編程,是函數式編程的一種衍生範型。通過先行編譯方式和運行期動態代理實現程式功能的統一維護的一種技術。主要的功能是:日誌記錄,效能統計,安全控制,交易處理,異常處理等等。主要的意圖是:將日誌記錄,效能統計,安全控制,交易處理,異常處理等代碼從商務邏輯代碼中劃分出來,通過對這些行為的分離,我們希望可以將它們獨立到非指導商務邏輯的方法中,進而改變這些行為的時候不影響商務邏輯的代碼。
運用
這裡舉個例子,我們有個方法sumA:andB:, 用來返回ab之和的一個字串,我們在這個方法前和方法後都增加個一段代碼
- 在運行方法前我們把參數改成2和3, 當然這裡是示範用,實際用的時候別改參數,不然其他同事真的要罵人了
- 在運行方法後我們輸出傳入的參數和返回值
- (void)clickTestAop:(id)sender{ AopTestM *test = [[AopTestM alloc] init]; NSLog(@"run1"); [test sumA:1 andB:2]; NSString *before = [XYAOP interceptClass:[AopTestM class] beforeExecutingSelector:@selector(sumA:andB:) usingBlock:^(NSInvocation *invocation) { int a = 3; int b = 4; [invocation setArgument:&a atIndex:2]; [invocation setArgument:&b atIndex:3]; NSLog(@"berore fun. a = %d, b = %d", a , b); }]; NSString *after = [XYAOP interceptClass:[AopTestM class] afterExecutingSelector:@selector(sumA:andB:) usingBlock:^(NSInvocation *invocation) { int a; int b; NSString *str; [invocation getArgument:&a atIndex:2]; [invocation getArgument:&b atIndex:3]; [invocation getReturnValue:&str]; NSLog(@"after fun. a = %d, b = %d, sum = %@", a , b, str); }]; NSLog(@"run2"); [test sumA:1 andB:2]; [XYAOP removeInterceptorWithIdentifier:before]; [XYAOP removeInterceptorWithIdentifier:after]; NSLog(@"run3"); [test sumA:1 andB:2];} - (NSString *)sumA:(int)a andB:(int)b{ int value = a + b; NSString *str = [NSString stringWithFormat:@"fun running. sum : %d", value]; NSLog(@"%@", str); return str;}
我們執行這段代碼的時候,大夥猜猜結果是啥.結果如下
2014-10-28 22:52:47.215 JoinShow[3751:79389] run12014-10-28 22:52:52.744 JoinShow[3751:79389] fun running. sum : 32014-10-28 22:52:52.745 JoinShow[3751:79389] run22014-10-28 22:52:52.745 JoinShow[3751:79389] berore fun. a = 3, b = 42014-10-28 22:52:52.745 JoinShow[3751:79389] fun running. sum : 72014-10-28 22:52:52.745 JoinShow[3751:79389] after fun. a = 3, b = 4, sum = fun running. sum : 72014-10-28 22:52:52.746 JoinShow[3751:79389] run32014-10-28 22:52:52.746 JoinShow[3751:79389] fun running. sum : 3
實現原理
用Objective-C強大的runtime.
我們知道當給一個對象發送一個方法的時候, 如果當前類和父類都沒實現該方法的時候就會走轉寄流程
- 動態方法解析 -> 快速訊息轉寄 -> 標準訊息轉寄
迷茫的同學請搜 "Objective-C 訊息轉寄".
瞭解了訊息轉寄,那麼我們aop的思路就來了,我們是先幹掉原本的方法funa,這樣當給對象發送方法的時候就會走轉寄流程,我們再hook了對象的快速訊息轉寄方法,把實現funa的對象指成我們的aop對象, 最後在aop對象的標準訊息轉寄裡執行before instead after方法.
具體的代碼歡迎大夥去github下載, 記得給咱點個star
link https://github.com/uxyheaven/XYQuickDevelop
在代碼裡搜 XYAOP.h
相關一些方法介紹
介紹一些用到的runtime方法
// 給 cls 添加一個新方法BOOL class_addMethod ( Class cls, SEL name, IMP imp, const char *types);// 替換 cls 裡的一個方法的實現IMP class_replaceMethod ( Class cls, SEL name, IMP imp, const char *types);// 返回 cls 的指定方法Method class_getInstanceMethod ( Class cls, SEL name);// 設定一個方法的實現IMP method_setImplementation ( Method m, IMP imp);// 返回 cls 裡的 name 方法的實現IMP class_getMethodImplementation ( Class cls, SEL name);
對於Objective C編程:
1.可以運行,需要gcc編譯器編譯.
2.據我所知沒有.
3.不能
4.語言沒有優略,只有更適用與某一方面,如C和彙編適用於底層硬體,java更適用與網路等等。
OC火起來有其特殊性,因為IOS的類庫是OC寫的,說實話,它並不是一門很好的語言,用起來比較繁瑣。以上是個人愚見。
objective-c與Cpp怎混合編程?
首先你的工程要支援C++的寫法,有兩種方法可以實現:
(1)把appDelegate.m檔案還成appDelegate.mm檔案
(2)選擇工程的Build Settings ->Compiler for c/c++/objective-c 設定為Apple LLVM Compiler 3.0
再有就是把有C++實現方法的.m檔案的類型設定為obejctive c++ source。
這樣就可以實現混編了。
希望能協助到你。