Last active
December 19, 2015 02:28
-
-
Save advantis/5883078 to your computer and use it in GitHub Desktop.
Proxy that emulates Responsibility Chain pattern
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <Foundation/Foundation.h> | |
@interface ADVResonsibilityChainProxy : NSObject | |
- (id) initWithHandlers:(NSArray *)hanlders; | |
@end |
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import "ADVResonsibilityChainProxy.h" | |
@implementation ADVResonsibilityChainProxy | |
{ | |
NSArray *_handlers; | |
} | |
#pragma mark - ADVResonsibilityChainProxy | |
- (id) initWithHandlers:(NSArray *)hanlders | |
{ | |
if ((self = [super init])) | |
{ | |
_handlers = hanlders; | |
} | |
return self; | |
} | |
- (id) childRespondingToSelector:(SEL)selector | |
{ | |
for (id handler in _handlers) | |
{ | |
if ([handler respondsToSelector:selector]) return handler; | |
} | |
return nil; | |
} | |
#pragma mark - NSObject | |
- (BOOL) respondsToSelector:(SEL)selector | |
{ | |
return [super respondsToSelector:selector] || [self childRespondingToSelector:selector]; | |
} | |
- (id) forwardingTargetForSelector:(SEL)selector | |
{ | |
return [self childRespondingToSelector:selector] ?: [super forwardingTargetForSelector:selector]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment