Skip to content

Instantly share code, notes, and snippets.

@seyhagithub
Created October 1, 2018 10:05
Show Gist options
  • Save seyhagithub/9edb3714ce6c4971e239a8c6575dcf83 to your computer and use it in GitHub Desktop.
Save seyhagithub/9edb3714ce6c4971e239a8c6575dcf83 to your computer and use it in GitHub Desktop.
Dependency Inversion Principle
protocol NetworkLayerType {
func request() -> [String]
}
class ViewController {
var models: [String]!
let networkLayer: NetworkLayerType
init(networkLayer: NetworkLayerType) {
self.networkLayer = networkLayer
}
func viewDidLoad() {
models = networkLayer.request()
debugPrint(models.count)
}
}
class NetworkLayer: NetworkLayerType {
func request() -> [String] {
// Example this is real network request
return ["product1", "product2", "product3"]
}
}
class MockNetworkLayer: NetworkLayerType {
func request() -> [String] {
// Example Fake data
return ["FakeProduct1", "FakeProduct2", "FakeProduct3"]
}
}
//let networkLayer = NetworkLayer()
//
//
//let viewController = ViewController(networkLayer: networkLayer)
//viewController.viewDidLoad()
let mockNetworkLayer = MockNetworkLayer()
let viewController = ViewController(networkLayer: mockNetworkLayer)
viewController.viewDidLoad()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment