Created
December 25, 2015 10:56
-
-
Save oleksii-demedetskyi/86d409a6d7662f6b6500 to your computer and use it in GitHub Desktop.
Service locator in swift
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
| class Murzik : Cat { | |
| func meow() -> String { | |
| return "Meeooow" | |
| } | |
| } | |
| class Muhtar : Dog { | |
| func bark() -> String { | |
| return "Woof" | |
| } | |
| } |
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
| class Client { | |
| let cat : Cat | |
| let dog : Dog | |
| init(locator: ServiceLocator) { | |
| cat = locator.getService() | |
| dog = locator.getService() | |
| } | |
| func makeNoise() -> String { | |
| return "Cat goes \(cat.meow()), dog goes \(dog.bark())" | |
| } | |
| } |
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
| class Client { | |
| let cat : Cat | |
| let dog : Dog | |
| func makeNoise() -> String { | |
| return "Cat goes \(cat.meow()), dog goes \(dog.bark())" | |
| } | |
| } |
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
| protocol ServiceContainer { | |
| var locator: ServiceLocator {get} | |
| func registerService<T>(service: T) | |
| } |
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
| protocol ServiceLocator { | |
| func getService<T>() -> T; | |
| } |
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
| protocol Cat { | |
| func meow() -> String | |
| } | |
| protocol Dog { | |
| func bark() -> String | |
| } |
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
| //: Playground - noun: a place where people can play | |
| protocol Cat { | |
| func meow() -> String | |
| } | |
| protocol Dog { | |
| func bark() -> String | |
| } | |
| class Murzik : Cat { | |
| func meow() -> String { | |
| return "Meeooow" | |
| } | |
| } | |
| class Muhtar : Dog { | |
| func bark() -> String { | |
| return "Woof" | |
| } | |
| } | |
| protocol ServiceLocator { | |
| func getService<T>() -> T; | |
| } | |
| class Client { | |
| let cat : Cat | |
| let dog : Dog | |
| init(locator: ServiceLocator) { | |
| cat = locator.getService() | |
| dog = locator.getService() | |
| } | |
| func makeNoise() -> String { | |
| return "Cat goes \(cat.meow()), dog goes \(dog.bark())" | |
| } | |
| } | |
| protocol ServiceContainer { | |
| var locator: ServiceLocator {get} | |
| func registerService<T>(service: T) | |
| } | |
| class SimpleContainer : ServiceContainer, ServiceLocator { | |
| var locator: ServiceLocator { return self } | |
| private var registry : [String: Any] = [:] | |
| func registerService<T>(service: T) { | |
| let key = "\(T.self)" | |
| registry[key] = service | |
| } | |
| func getService<T>() -> T { | |
| let key = "\(T.self)" | |
| return registry[key] as! T | |
| } | |
| } | |
| let serviceContainer = SimpleContainer() as ServiceContainer | |
| serviceContainer.registerService(Murzik() as Cat) | |
| serviceContainer.registerService(Muhtar() as Dog) | |
| let client = Client(locator: serviceContainer.locator) | |
| client.makeNoise() // "Cat goes Meeooow, dog goes Woof" |
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
| class SimpleContainer : ServiceContainer, ServiceLocator { | |
| var locator: ServiceLocator { return self } | |
| private var registry : [String: Any] = [:] | |
| func registerService<T>(service: T) { | |
| let key = "\(T.self)" | |
| registry[key] = service | |
| } | |
| func getService<T>() -> T { | |
| let key = "\(T.self)" | |
| return registry[key] as! T | |
| } | |
| } |
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
| let client = Client(locator: serviceContainer.locator) | |
| client.makeNoise() // "Cat goes Meeooow, dog goes Woof" |
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
| let serviceContainer = SimpleContainer() as ServiceContainer | |
| serviceContainer.registerService(Murzik() as Cat) | |
| serviceContainer.registerService(Muhtar() as Dog) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment