High level modules should not depend upon low level modules. Both should depend upon abstractions. Abstractions should not depend upon details. Details should depend upon abstractions.
상위 모듈은 하위 모듈에 의존에서는 안되고, 상위, 하위 모듈 모두 ‘추상’을 의존해야 한다 ’추상’은 ‘디테일’을 의존하면 안되고, ‘디테일’은 ‘추상’에 의존해야 함
‘좋은’ 디자인, ‘좋지 않은’ 디자인의 기준은 없지만,
’좋지 않은’ 디자인이 갖는 특징들이 있음
변경을 주기 어려움, 한 곳을 바꾸면 다른 부분을 망가트림
- OCP를 지키지 않았을 때 어떤 부분에 확장이 필요할 때, 다른 부분도 변경해야 함
- 어떤 구조가
‘추상’이 아닌 다른 구조에 의존할 때
한 곳에서 변경을 주면 다른 부분도 변경해야 함
- OCP가 지켜지지 않았을 때
- 어떤 구조가
‘추상’이 아닌 다른 구조에 의존할 때
여기저기 연결되어있기 때문에 재사용이 어려움
- 어떤 구조가
‘추상’이 아닌 다른 구조에 의존할 때
이런 이유들 때문에, DIP가 좋은 디자인에 직접적으로 영향을 미침
OCP와 LSP에서의 해결책은 모두 Abstraction
즉, DIP를 사용했을 때 OCP와 LSP를 지킬 수 있다
struct Product {
let name: String
let cost: Int
let image: UIImage
}
final class Network {
private let urlSession = URLSession(configuration: .default)
func getProducts(for userId: String, completion: @escaping ([Product]) -> Void) {
guard let url = URL(string: "baseURL/products/user/\\(userId)") else {
completion([])
return
}
var request = URLRequest(url: url)
request.httpMethod = "GET"
urlSession.dataTask(with: request) { (data, response, error) in
DispatchQueue.main.async {
completion([Product(name: "Just an example", cost: 1000, image: UIImage())])
}
}
}
}