Skip to content

Instantly share code, notes, and snippets.

@fuzza
Forked from oleksii-demedetskyi/AppDelegate.m
Last active May 14, 2016 18:13
Show Gist options
  • Select an option

  • Save fuzza/5cbd51eea913a3b39ed9 to your computer and use it in GitHub Desktop.

Select an option

Save fuzza/5cbd51eea913a3b39ed9 to your computer and use it in GitHub Desktop.
Service Locator
- didFinishAppLaunch
{
ServiceLocator* serviceLocator = [ServiceLocator new];
SomeService* someService = [SomeService new];
[serviceLocator useObject:someService asServiceForKind:[someService class]];
SomeViewController* vc = [SomeViewController new];
vc.serviceLocator = serviceLocator;
}
@interface ServiceLocator: NSObject
- (void)useObject:(id)object asServiceForKind:(Class)serviceClass;
- (id)serviceOfKind:(Class)serviceClass;
@end
@interface SomeService: NSOject
- (void)doStuff;
@end
#import "ServiceLocator.h"
@interface ServiceLocator(SomeService)
@property (nonatomic, readonly) SomeService* someService;
@end
@implementation SomeService
- (void)doStuff {};
@end
@implementation ServiceLocator(SomeService)
- (SomeService*)somService
{
return [self serviceOfKind:[SomeService class]];
}
@end
#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