-
-
Save fuzza/5cbd51eea913a3b39ed9 to your computer and use it in GitHub Desktop.
Service Locator
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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