Junior — Middle
How is dependency management between components ensured in the system?
sobes.tech AI
Answer from AI
Dependency management between components in iOS systems is typically ensured by the following methods:
-
Dependency Injection — passing dependencies to a component via constructor, properties, or methods. This allows easy replacement of dependencies for testing and improves modularity.
-
Service Locator — a pattern where components request dependencies from a centralized object (service locator).
-
Use of protocols — to abstract dependencies and ensure loose coupling.
-
DI frameworks — such as Swinject, which automate dependency injection.
Example of dependency injection via constructor:
protocol NetworkService {
func fetchData()
}
class APIService: NetworkService {
func fetchData() {
// Implementation
}
}
class ViewModel {
private let networkService: NetworkService
init(networkService: NetworkService) {
self.networkService = networkService
}
func loadData() {
networkService.fetchData()
}
}
let apiService = APIService()
let viewModel = ViewModel(networkService: apiService)