Created
October 1, 2018 10:05
-
-
Save seyhagithub/9edb3714ce6c4971e239a8c6575dcf83 to your computer and use it in GitHub Desktop.
Dependency Inversion Principle
This file contains 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 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