티스토리 뷰

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

'iOS' 카테고리의 다른 글

[번역] WatchKit  (0) 2022.01.20
iOS status bar style 변경하기  (1) 2021.11.18
iOS TextField memory leak  (0) 2021.07.16
iOS App Life Cycle  (2) 2021.07.11
swift static, class, final class  (0) 2020.08.02