iOS
Bastard Injection
경표다
2021. 8. 17. 10:50
작성중...
.
.
.
.
.
.
.
.
.
class DocumentOpener {
let urlOpener: URLOpening
init(application: URLOpening = UIApplication.shared) {
self.urlOpener = urlOpener
}
func open(_ document: Document, mode: OpenMode) {
let modeString = mode.rawValue
let url = URL(string: "myappscheme://open?id=\(document.identifier)&mode=\(modeString)")!
if urlOpener.canOpenURL(url) {
urlOpener.open(url, options: [:], completionHandler: nil)
} else {
handleURLError()
}
}
}
장점
- The test does not need an instance of UIViewController to test the
DocumentOpener
- The test is non-deterministic
- The class allows for dependency injection through parameterization
- The URLOpening protocol decouples the DocumentOpener code from a specific implementation
- DocumentOpener is simple and elegant to use because you don’t need to pass in a dependency
- If you want to use a different default you only need to change it in one place
단점
- The DocumentOpener is still coupled to UIApplication
- It presents a practice that is ill-advised in a different circumstance
왜 안티패턴??
애플은 왜 이렇게 씀??
참고
Why did Apple Advocate for Bastard Injection?
Implementing Dependency Injection in Swift [Tutorial]
Singleton Pattern * Dependency Injection * Service Locator