Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created November 10, 2011 05:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jspahrsummers/1354206 to your computer and use it in GitHub Desktop.
Save jspahrsummers/1354206 to your computer and use it in GitHub Desktop.
Sketch of aspect-oriented programming in Objective-C
/* 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