Add "Sharing helper methods via protocol extension" section

This commit is contained in:
Wojciech Nagrodzki 2019-12-10 20:22:33 +01:00
parent bc4ee7423c
commit 809fab8da7
Signed by: wnagrodzki
GPG key ID: E9D0EB0302264569

View file

@ -242,3 +242,49 @@ operation.onComplete { [weak self] result in
Do not access singleton instance directly via static method - `Singleton.sharedInstance()`. Decouple from it with dependency injection.
> If a singleton is used directly there is no way to perform unit tests in isolation from it. Moreover, it may introduce unexpected shared state between unit tests if they are executed in one run.
### Sharing helper methods via protocol extension
Place shared helper methods in extension of dedicated separate protocol.
```swift
// MyProtocol.swift
protocol MyProtocol {
func method()
}
protocol MyProtocolBase { }
extension MyProtocolBase {
func helperMethod() { }
}
```
```swift
// MyProtocolImplA.swift
class MyProtocolImplA: MyProtocol {
func method() {
// helperMethod() can be called here due to MyProtocolBase conformance
}
}
extension MyProtocolImplA: MyProtocolBase { }
```
```swift
// MyProtocolImplB.swift
class MyProtocolImplB: MyProtocol {
func method() {
// helperMethod() can be called here due to MyProtocolBase conformance
}
}
extension MyProtocolImplB: MyProtocolBase { }
```
> This is to avoid exposing helper methods in the main protocol and thus polluting it.