Sobes.tech
Junior — Middle

What is the significance of the D letter in the SOLID principles when developing software?

sobes.tech AI

Answer from AI

The D in SOLID principles stands for Dependency Inversion Principle. It states that:

  • High-level modules should not depend on low-level modules. Both should depend on abstractions.
  • Abstractions should not depend on details. Details should depend on abstractions.

This helps create flexible and easily extendable architectures, where components are connected through interfaces rather than directly to each other.

Example in Swift:

protocol DataService {
    func fetchData() -> String
}

class NetworkService: DataService {
    func fetchData() -> String {
        return "Data from network"
    }
}

class ViewModel {
    private let service: DataService

    init(service: DataService) {
        self.service = service
    }

    func getData() -> String {
        return service.fetchData()
    }
}

let networkService = NetworkService()
let viewModel = ViewModel(service: networkService)
print(viewModel.getData()) // Outputs: Data from network

Here, ViewModel depends not on the specific implementation of NetworkService, but on the abstraction DataService.

What is the significance of the D letter in the SOLID… - sobes.tech