Created
November 24, 2014 15:08
-
-
Save oleksii-demedetskyi/3879c54efb66eb181391 to your computer and use it in GitHub Desktop.
ServiceLocator Pattern for iOS
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
- didFinishAppLaunch | |
{ | |
ServiceLocator* serviceLocator = [ServiceLocator new]; | |
SomeService* someService = [SomeService new]; | |
[serviceLocator useObject:someService asServiceForKind:[someService class]]; | |
SomeViewController* vc = [SomeViewController new]; | |
vc.serviceLocator = serviceLocator; | |
} |
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
@interface ServiceLocator: NSObject | |
- (void)useObject:(id)object asServiceForKind:(Class)serviceClass; | |
- (id)serviceOfKind:(Class)serviceClass; | |
@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
@interface SomeService: NSOject | |
- (void)doStuff; | |
@end | |
#import "ServiceLocator.h" | |
@interface ServiceLocator(SomeService) | |
@property (nonatomic, readonly) SomeService* someService; | |
@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
@implementation SomeService | |
- (void)doStuff {}; | |
@end | |
@implementation ServiceLocator(SomeService) | |
- (SomeService*)somService | |
{ | |
return [self serviceOfKind:[SomeService class]]; | |
} | |
@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
#import "SomeService.h" | |
- (void)viewDidLoad | |
{ | |
[self.serviceLocator.someService doStuff]; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment