Skip to content

Instantly share code, notes, and snippets.

@oleksii-demedetskyi
Created November 24, 2014 15:08
Show Gist options
  • Save oleksii-demedetskyi/3879c54efb66eb181391 to your computer and use it in GitHub Desktop.
Save oleksii-demedetskyi/3879c54efb66eb181391 to your computer and use it in GitHub Desktop.
ServiceLocator Pattern for iOS
- 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