Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Aspect Oriented Programming
Search
Weizhong Yang
September 12, 2013
Technology
260
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Aspect Oriented Programming
Weizhong Yang
September 12, 2013
More Decks by Weizhong Yang
See All by Weizhong Yang
導入 Flutter 前你應該知道的事
zonble
7
1.3k
那些年,被蘋果 Ban 掉的 API
zonble
0
190
為視障朋友打造行動應用
zonble
16
1.4k
Mac OS X 與 iOS 的 Audio API
zonble
11
1.1k
大家來寫貪食蛇
zonble
7
4.5k
Debug Debug
zonble
6
460
Retina Mac
zonble
3
290
HTML 5 Native Drag
zonble
3
560
Other Decks in Technology
See All in Technology
go-sqletch
moznion
0
120
全人類(ほぼ)AWS Organizations の上でAWSを利用している、その世界を知る話
htan
1
130
契約書画像からの情報抽出に特化した視覚接地モデルを育てる話
sansantech
PRO
1
160
プロダクト価値を、 チームが使える判断軸に変える
vivion
0
140
AI 時代の Azure エンジニアリング ~ 私たちは何を磨き、何を任せるのか ~
chack411
1
550
ブラウザとサーバーから考えるAI時代のフロントエンド監視
shogo4131
3
630
メルペイ 会計システム概要と歴史
mewuto
0
200
Execution in the Kingdom of Agents: Reflections on Abstraction and Complexity
bcantrill
0
890
AIがコードを書く時代に、エンジニアは何で自分の価値を証明するのか / In an era where AI writes code how do engineers prove their value
carta_engineering
1
680
検証フェーズはAIの回答を判断するための学習機会
toru_kubota
2
550
AgentCoreで実践するハーネスエンジニアリング
yakumo
1
290
会社説明資料
persolserverworks
0
9.6k
Featured
See All Featured
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
11
1.1k
sira's awesome portfolio website redesign presentation
elsirapls
0
450
Fashionably flexible responsive web design (full day workshop)
malarkey
409
67k
Bootstrapping a Software Product
garrettdimon
PRO
306
120k
Leveraging LLMs for student feedback in introductory data science courses - posit::conf(2025)
minecr
1
420
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
38
3k
Everyday Curiosity
cassininazir
0
350
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.5k
Building a Scalable Design System with Sketch
lauravandoore
464
34k
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
720
The Organizational Zoo: Understanding Human Behavior Agility Through Metaphoric Constructive Conversations (based on the works of Arthur Shelley, Ph.D)
kimpetersen
PRO
0
490
Transcript
Aspect Oriented Programming 楊維中 a.k.a zonble
[email protected]
Thursday, September 12,
zonble ฌᖯ؛ۇ Thursday, September 12,
ᖯ৵ʔ݊ɓ˂ிϓٙ Unmaintainable 的 code 也是 Thursday, September 12,
⼀一開始,程式邏輯就只有插 ⼊入資料⽽而已… - (void)appendData:(NSData *)inData { [_data appendData:inData]; } Thursday,
September 12,
後來,我們想確定傳⼊入的不 是 nil 或空資料 - (void)appendData:(NSData *)inData { NSParameterAssert(inData !=
nil); NSParameterAssert([inData length]); length = [_data length]; [_data appendData:inData]; NSAssert(length != [_data length]); } Thursday, September 12,
後來,我們覺得 Release Build 也應該防⽌止輸⼊入空資料… - (void)appendData:(NSData *)inData { NSParameterAssert(inData !=
nil); NSParameterAssert([inData length]); if (!inData) { return; } if (![inData length]) { return; } length = [_data length]; [_data appendData:inData]; NSAssert(length != [_data length]); } Thursday, September 12,
後來,我們發現這個 method 可能會在很多 thread 呼叫… - (void)appendData:(NSData *)inData { NSParameterAssert(inData
!= nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); } Thursday, September 12,
後來,在 Debug Build 裡頭要 加上 TestFlight CheckPoint - (void)appendData:(NSData *)inData
{ NSParameterAssert(inData != nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #endif } Thursday, September 12,
Release Build 則要加上 Flurry Event Log… - (void)appendData:(NSData *)inData {
NSParameterAssert(inData != nil); NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #else [Flurry logEvent:@"APPEND_DATA"]; #endif } Thursday, September 12,
還有 Google Analytics… - (void)appendData:(NSData *)inData { NSParameterAssert(inData != nil);
NSParameterAssert([inData length]); if (!inData) { NSLog(@"inData is nil!"); return; } if (![inData length]) { NSLog(@"Length of inData is 0!"); return; } length = [_data length]; [_lock lock]; [_data appendData:inData]; [_lock unlock]; NSAssert(length != [_data length]); #if DEBUG [TestFlight passCheckpoint:@"APPEND_DATA"]; #else [Flurry logEvent:@"APPEND_DATA"]; id<GAITracker>tracker = [[GAI sharedInstance] defaultTracker]; [tracker send: [[GAIDictionaryBuilder createEventWithCategory:@"data" withAction:@"append" withLabel:@"user_data" withValue:nil] build]]; #endif } Thursday, September 12,
每件事情都很重要 • 確定傳⼊入參數的型態與內容 • 確定真的有插⼊入資料 • 加 Lock • 加
Debug Log • 加 TestFlight Log、Flurry Log… Thursday, September 12,
23 ⾏行 code 中, ਂ͍ԫ̥ٙϞɓБf 這還不是最糟的… Thursday, September 12,
更糟的是… 不只⼀一個 method 這樣 Thursday, September 12,
ᒔึॎᕸόٙࠠ͜ Thursday, September 12,
上個世紀末,就有⼈人覺得… வᅵʔБ 接下來的故事請⾃自⼰己查 Wikipedia Thursday, September 12,
AOP 的基本想法: Ҫ၇ౢආԸٙ Code פᕎԨ܆ༀ ⽅方法很多 Thursday, September 12,
Cross-cutting concerns …就是這些 • 確定傳⼊入參數的型態與內容 • 確定真的有插⼊入資料 • 加 Lock
• 加 Debug Log • 加 TestFlight Log、Flurry Log… Thursday, September 12,
Advice 在原本程式前後 插⼊入的程式碼 Thursday,
September 12,
Decorator Syntax …Objective C 不⽀支援 Thursday, September 12,
Decorator Syntax def add(x): print "x:" + str(x) result =
x + 1 print "result:" + str(result) return result Thursday, September 12,
Decorator Syntax def log(func): def inner_func(*a): print "args:" + str(a)
result = func(*a) print "result:" + str(result) return inner_func @log def add(x): return x + 1 Thursday, September 12,
Objective-C 特性 Message Forwarding 當⼀一個物件不⽀支援某個 Selector 的時 候,這個物件可以決定把 Selector 交給
其他的物件。 Thursday, September 12,
Message Forwarding - (void)forwardInvocation:(NSInvocation *)anInvocation { if ([someOtherObject respondsToSelector: [anInvocation
selector]]) // Advice here [anInvocation invokeWithTarget:someOtherObject]; // Advice here else [super forwardInvocation:anInvocation]; } See: Objective-C Runtime Programming Guide Thursday, September 12,
AOP-in-Objective-C https://github.com/moszi/AOP-in- Objective-C Thursday, September 12,
Method Swizzling Thursday, September 12,
Objective-C 第⼀一課 • ObjC 物件都是 C Structure • ObjC method
都是 C function pointer • 系統有⼀一個動態的索引表格,決定執⾏行 某個 selector 時,要對應到哪個 C function • Selector 就是索引表中的 key,型態為 C 字串 Thursday, September 12,
Objective-C 第⼆二課 • ⼀一個 Class 有哪些 method,都是在 runtime 的時候建⽴立的 •
所以可以在 runtime 新增新的 method, 例如使⽤用 category 語法 • 也可以直接使⽤用 runtime API 把已經存在 的 method 換掉 Thursday, September 12,
<objc/runtime.h> BOOL class_addMethod(Class cls, SEL name, IMP imp, const char
*types); IMP method_setImplementation(Method method, IMP imp); Thursday, September 12,
Thursday, September 12,
Thursday, September 12,
Thursday, September 12,
AOP-for-Objective-C https://github.com/ndcube/AOP-for- Objective-C/ 在繼承關係的部份有些 bug,我⾃自⼰己有 先修,但是還沒有送 pull request… Thursday, September
12,
Thank you! Thursday, September 12,