Created
December 15, 2017 13:54
-
-
Save dev4dev/a3a8af54d0ede207c1673b8902b2f062 to your computer and use it in GitHub Desktop.
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
// some shared services | |
protocol NetworkService: class { | |
func get() -> Any | |
} | |
class TestnetworkService: NetworkService { | |
func get() -> Any { | |
return "GET IT" | |
} | |
} | |
// server specific endpoints | |
protocol PersonDataEndpoint: class { | |
init(networkService: NetworkService) | |
func getPerson() -> Any | |
} | |
// impl 1 | |
final class TestPersonDataEndpoint: PersonDataEndpoint { | |
let networkService: NetworkService | |
init(networkService: NetworkService) { | |
self.networkService = networkService | |
} | |
func getPerson() -> Any { | |
return "\(networkService.get()) -> Bob" | |
} | |
} | |
// impl 2 | |
final class OloPersonDataEndpoint: PersonDataEndpoint { | |
let networkService: NetworkService | |
init(networkService: NetworkService) { | |
self.networkService = networkService | |
} | |
func getPerson() -> Any { | |
return "\(networkService.get()) -> Ololo" | |
} | |
} | |
// specific service wrapper | |
class PersonDataService { | |
let provider: PersonDataEndpoint | |
init(provider: PersonDataEndpoint) { | |
self.provider = provider | |
} | |
func getPerson() -> Any { | |
return provider.getPerson() | |
} | |
} | |
// core | |
class Core { | |
let networkService: NetworkService = TestnetworkService() | |
let personDataService: PersonDataService | |
init(personDataClass: PersonDataEndpoint.Type) { | |
personDataService = PersonDataService(provider: personDataClass.init(networkService: networkService)) | |
} | |
} | |
// -- | |
let core = Core(personDataClass: arc4random_uniform(2) == 1 ? OloPersonDataEndpoint.self : TestPersonDataEndpoint.self) | |
print(core.personDataService.getPerson()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment