Created
November 10, 2011 05:49
-
-
Save jspahrsummers/1354206 to your computer and use it in GitHub Desktop.
Sketch of aspect-oriented programming in Objective-C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Logger.m */ | |
@aspect Logger | |
// called for any selector | |
- (void)before { | |
// '_cmd' is special -- the selector for the actual method with the advice | |
// applied to it | |
NSLog(@"starting -[%@ %@]", self, _cmd); | |
} | |
// only called for 'deposit:' | |
- (void)beforeDeposit:(NSDecimalNumber *)amount { | |
NSLog(@"depositing %@ to %@", amount, self); | |
} | |
// called for any selector | |
- (void)after { | |
NSLog(@"done with -[%@ %@]", self, _cmd); | |
} | |
@end | |
/* BankAccount.h */ | |
@interface BankAccount : NSObject <Logger> { | |
} | |
- (void)deposit:(NSDecimalNumber *)amount; | |
- (void)withdraw:(NSDecimalNumber *)amount; | |
@end | |
/* Transaction.m */ | |
@aspect Transaction | |
- (void)before { | |
// blah blah blah transaction code here | |
} | |
- (void)after { | |
// blah blah blah transaction code here | |
} | |
@end | |
/* BankAccount+TransactionExtensions.h */ | |
@interface BankAccount (TransactionExtensions) <Transaction> | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment