Skip to content

Instantly share code, notes, and snippets.

@dev4dev dev4dev/test.swift
Created Dec 15, 2017

Embed
What would you like to do?
// 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
You can’t perform that action at this time.