mirror of
https://github.com/jphong1111/awesome-ios-developer.git
synced 2025-01-30 09:31:35 +07:00
update DI
This commit is contained in:
parent
87915b885c
commit
c21a8efa5f
48
README.md
48
README.md
@ -158,6 +158,54 @@ weak var delegate: SomeProtocol?
|
|||||||
```
|
```
|
||||||
## Dependency Injection
|
## Dependency Injection
|
||||||
|
|
||||||
|
Dependency injection is a pattern that can be used to eliminate the need for singletons in a project
|
||||||
|
|
||||||
|
|
||||||
|
1. Raise Transparency
|
||||||
|
2. Improve Testability
|
||||||
|
|
||||||
|
### Type of Dependency Injection
|
||||||
|
|
||||||
|
1. initializer injection
|
||||||
|
|
||||||
|
``` swift
|
||||||
|
class DataManager {
|
||||||
|
|
||||||
|
private let serializer: Serializer
|
||||||
|
|
||||||
|
init(serializer: Serializer) {
|
||||||
|
self.serializer = serializer
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
2. property injection
|
||||||
|
|
||||||
|
```swift
|
||||||
|
import UIKit
|
||||||
|
|
||||||
|
class ViewController: UIViewController {
|
||||||
|
|
||||||
|
var requestManager: RequestManager?
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
3. method injection
|
||||||
|
|
||||||
|
```swift
|
||||||
|
import Foundation
|
||||||
|
|
||||||
|
class DataManager {
|
||||||
|
|
||||||
|
func serializeRequest(request: Request, withSerializer serializer: Serializer) -> Data? {
|
||||||
|
...
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
[Nuts and Bolts of Dependency Injection in Swift](https://cocoacasts.com/nuts-and-bolts-of-dependency-injection-in-swift)
|
[Nuts and Bolts of Dependency Injection in Swift](https://cocoacasts.com/nuts-and-bolts-of-dependency-injection-in-swift)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user