Upload
-
View
1.215
Download
0
Embed Size (px)
Citation preview
Minimal Cake Pattern in Swiftkbkz.tech #11
吉村優
https://twitter.com/_yyu_http://qiita.com/yyu
https://github.com/y-yu
September 16, 2016(Commit ID: 5acb721)
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 1 / 21
自己紹介
Scala 書 仕事 従事趣味 LATEX 暗号技術Swift 最近 初心者
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 2 / 21
自己紹介
Scala 書 仕事 従事
趣味 LATEX 暗号技術Swift 最近 初心者
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 2 / 21
自己紹介
Scala 書 仕事 従事趣味 LATEX 暗号技術
Swift 最近 初心者
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 2 / 21
自己紹介
Scala 書 仕事 従事趣味 LATEX 暗号技術Swift 最近 初心者
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 2 / 21
DI(Dependency Injection) ?
Dependency Injection ?
“ Dependency 実際 使
Injection Dependency 使渡
Scala 最適 Dependency Injection 方法 考察 [1]”
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 3 / 21
DI(Dependency Injection) ?
Dependency Injection ?
“ Dependency 実際 使
Injection Dependency 使渡
Scala 最適 Dependency Injection 方法 考察 [1]”吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 3 / 21
DI
“
同士 疎結合動作 可能
依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”
?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
DI
“
同士 疎結合動作 可能
依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”
?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
DI
“ 同士 疎結合
動作 可能依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”
?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
DI
“ 同士 疎結合動作 可能
依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”
?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
DI
“ 同士 疎結合動作 可能
依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”
?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
DI
“ 同士 疎結合動作 可能
依存 化可能
Scala 最適 Dependency Injection 方法 考察 [1]”?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 4 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“
Swinject 用 動的 DICake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”
以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“
Swinject 用 動的 DICake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”
以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“ Swinject 用 動的 DI
Cake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”
以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“ Swinject 用 動的 DICake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”
以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“ Swinject 用 動的 DICake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Swift 代表的 DI手法
Swift 代表的 DI手法
“ Swinject 用 動的 DICake Pattern 用 静的 DI
Dependency Injection in Swift 2.x[2]”以外 ?
Cake Pattern 仲間 “Minimal Cake Pattern” 紹介!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 5 / 21
Example
機能 作
HashPasswordService鍵付 関数∗ 化 機能
必要 機能
設定 読 込 機能化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
機能 作HashPasswordService
鍵付 関数∗ 化 機能
必要 機能
設定 読 込 機能化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
機能 作HashPasswordService
鍵付 関数∗ 化 機能
必要 機能
設定 読 込 機能化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
機能 作HashPasswordService
鍵付 関数∗ 化 機能
必要 機能設定 読 込 機能
化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
機能 作HashPasswordService
鍵付 関数∗ 化 機能
必要 機能設定 読 込 機能
化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
機能 作HashPasswordService
鍵付 関数∗ 化 機能
必要 機能設定 読 込 機能
化 機能
設定 読 込 ?
∗ 使 関数吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 6 / 21
Example
考
微妙設定 保存
一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存
一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
Example
考微妙
設定 保存一方、 時 読 込
IO 失敗 失敗 !
本実装 設定 読 込 、 時使
二 実装 必要
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 7 / 21
設定 読 込 機能設定 読 込 部分
1 ReadConfigService 作成protocol ReadConfigService {var configName: String { get }
func readSalt() -> String}
2 実装 投入extension ReadConfigService {private func readFile() -> Optional<String> {if {return Optional.Some( 中身)
} else {return Optional.None
}}
}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 8 / 21
設定 読 込 機能設定 読 込 部分
1 ReadConfigService 作成protocol ReadConfigService {var configName: String { get }
func readSalt() -> String}
2 実装 投入extension ReadConfigService {private func readFile() -> Optional<String> {if {return Optional.Some( 中身)
} else {return Optional.None
}}
}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 8 / 21
設定 読 込 機能設定 読 込 部分
1 ReadConfigService 作成protocol ReadConfigService {var configName: String { get }
func readSalt() -> String}
2 実装 投入extension ReadConfigService {private func readFile() -> Optional<String> {if {return Optional.Some( 中身)
} else {return Optional.None
}}
}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 8 / 21
設定 読 込 機能4 実装 ReadConfigServiceImpl 作成
class ReadConfigServiceImpl: ReadConfigService {let configName: String
init(_ str: String) {configName = str
}
func readSalt() -> String {// 本当 ……return readFile()!
}}
設定 引数 受 取 、読 込
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 9 / 21
設定 読 込 機能4 実装 ReadConfigServiceImpl 作成
class ReadConfigServiceImpl: ReadConfigService {let configName: String
init(_ str: String) {configName = str
}
func readSalt() -> String {// 本当 ……return readFile()!
}}
設定 引数 受 取 、読 込
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 9 / 21
設定 読 込 機能5 実装 ReadConfigServiceMockImpl 作成
class ReadConfigServiceMockImpl: ReadConfigService {var configName: String = "dummy"let dummySalt: String
init(_ salt: String) {dummySalt = salt
}
func readSalt() -> String {return dummySalt
}}
設定 、 引数 与 値 必 返
、設定 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 10 / 21
設定 読 込 機能5 実装 ReadConfigServiceMockImpl 作成
class ReadConfigServiceMockImpl: ReadConfigService {var configName: String = "dummy"let dummySalt: String
init(_ salt: String) {dummySalt = salt
}
func readSalt() -> String {return dummySalt
}}
設定 、 引数 与 値 必 返
、設定 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 10 / 21
設定 読 込 機能5 実装 ReadConfigServiceMockImpl 作成
class ReadConfigServiceMockImpl: ReadConfigService {var configName: String = "dummy"let dummySalt: String
init(_ salt: String) {dummySalt = salt
}
func readSalt() -> String {return dummySalt
}}
設定 、 引数 与 値 必 返
、設定 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 10 / 21
設定 読 込 機能
6 依存 示 UsesReadConfigService
作成protocol UsesReadConfigService {var readConfigService: ReadConfigService { get }
}
使
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 11 / 21
設定 読 込 機能
6 依存 示 UsesReadConfigService
作成protocol UsesReadConfigService {var readConfigService: ReadConfigService { get }
}
使
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 11 / 21
化 機能次 、 化 部分 作成
1 作成protocol HashPasswordService: UsesReadConfigService {func hashBySha1(password: String) -> String
}
ReadConfigService 依存 示
2 実装 投入extension HashPasswordService {func hashBySha1(password: String) -> String {let data = password + readConfigService.readSalt()//return SHA1(data).toString()
}}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 12 / 21
化 機能次 、 化 部分 作成
1 作成protocol HashPasswordService: UsesReadConfigService {func hashBySha1(password: String) -> String
}
ReadConfigService 依存 示
2 実装 投入extension HashPasswordService {func hashBySha1(password: String) -> String {let data = password + readConfigService.readSalt()//return SHA1(data).toString()
}}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 12 / 21
化 機能次 、 化 部分 作成
1 作成protocol HashPasswordService: UsesReadConfigService {func hashBySha1(password: String) -> String
}
ReadConfigService 依存 示
2 実装 投入extension HashPasswordService {func hashBySha1(password: String) -> String {let data = password + readConfigService.readSalt()//return SHA1(data).toString()
}}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 12 / 21
化 機能次 、 化 部分 作成
1 作成protocol HashPasswordService: UsesReadConfigService {func hashBySha1(password: String) -> String
}
ReadConfigService 依存 示
2 実装 投入extension HashPasswordService {func hashBySha1(password: String) -> String {let data = password + readConfigService.readSalt()//return SHA1(data).toString()
}}
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 12 / 21
化 機能1 実装 作成
class HashPasswordSerivceImpl: HashPasswordService {let readConfigService: ReadConfigService
init(_ configFile: String) {readConfigService = ReadConfigServiceImpl(configFile)
}}
ReadConfigService 実装 DI
2 実装 作成class HashPasswordServiceTestImpl: HashPasswordService {var readConfigService: ReadConfigService =ReadConfigServiceMockImpl("dummySalt")
}
ReadConfigService 実装 DI
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 13 / 21
化 機能1 実装 作成
class HashPasswordSerivceImpl: HashPasswordService {let readConfigService: ReadConfigService
init(_ configFile: String) {readConfigService = ReadConfigServiceImpl(configFile)
}}
ReadConfigService 実装 DI
2 実装 作成class HashPasswordServiceTestImpl: HashPasswordService {var readConfigService: ReadConfigService =ReadConfigServiceMockImpl("dummySalt")
}
ReadConfigService 実装 DI
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 13 / 21
化 機能1 実装 作成
class HashPasswordSerivceImpl: HashPasswordService {let readConfigService: ReadConfigService
init(_ configFile: String) {readConfigService = ReadConfigServiceImpl(configFile)
}}
ReadConfigService 実装 DI
2 実装 作成class HashPasswordServiceTestImpl: HashPasswordService {var readConfigService: ReadConfigService =ReadConfigServiceMockImpl("dummySalt")
}
ReadConfigService 実装 DI
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 13 / 21
化 機能1 実装 作成
class HashPasswordSerivceImpl: HashPasswordService {let readConfigService: ReadConfigService
init(_ configFile: String) {readConfigService = ReadConfigServiceImpl(configFile)
}}
ReadConfigService 実装 DI
2 実装 作成class HashPasswordServiceTestImpl: HashPasswordService {var readConfigService: ReadConfigService =ReadConfigServiceMockImpl("dummySalt")
}
ReadConfigService 実装 DI
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 13 / 21
化 機能
11 作成class HashPasswordServiceMockImpl: HashPasswordService {var readConfigService: ReadConfigService =
ReadConfigServiceMockImpl("dummySalt")
func hashBySha1(password: String) -> String {return password + "_dummySalt"
}}
値 計算
実装 変更 、依存落
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 14 / 21
化 機能
11 作成class HashPasswordServiceMockImpl: HashPasswordService {var readConfigService: ReadConfigService =
ReadConfigServiceMockImpl("dummySalt")
func hashBySha1(password: String) -> String {return password + "_dummySalt"
}}
値 計算
実装 変更 、依存落
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 14 / 21
化 機能
11 作成class HashPasswordServiceMockImpl: HashPasswordService {var readConfigService: ReadConfigService =
ReadConfigServiceMockImpl("dummySalt")
func hashBySha1(password: String) -> String {return password + "_dummySalt"
}}
値 計算
実装 変更 、依存落
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 14 / 21
作成 HashPasswordService 作
import XCTest@testable import MCPExample
class HashPasswordServiceTest: XCTestCase {// 用 実装 使var sut: HashPasswordService = HashPasswordServiceTestImpl()
func testHashBySha1() {XCTAssert(sut.hashBySha1("hoge") == "ba5b31f489676c1545a9f175867274c8c21b8f8b")
}}
設定 読 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 15 / 21
作成 HashPasswordService 作import XCTest@testable import MCPExample
class HashPasswordServiceTest: XCTestCase {// 用 実装 使var sut: HashPasswordService = HashPasswordServiceTestImpl()
func testHashBySha1() {XCTAssert(sut.hashBySha1("hoge") == "ba5b31f489676c1545a9f175867274c8c21b8f8b")
}}
設定 読 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 15 / 21
作成 HashPasswordService 作import XCTest@testable import MCPExample
class HashPasswordServiceTest: XCTestCase {// 用 実装 使var sut: HashPasswordService = HashPasswordServiceTestImpl()
func testHashBySha1() {XCTAssert(sut.hashBySha1("hoge") == "ba5b31f489676c1545a9f175867274c8c21b8f8b")
}}
設定 読 !
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 15 / 21
DI 漏
次 DI 忘class HashPasswordServiceNgImpl: HashPasswordService {
func hashBySha1(password: String) -> String {return "ng"
}}
HashPasswordService.swift:41:7: Type 'HashPasswordServiceNgImpl'does not conform to protocol 'UsesReadConfigService'
実行前 DI漏 検出!
依存 増 効果的!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 16 / 21
DI 漏次 DI 忘
class HashPasswordServiceNgImpl: HashPasswordService {func hashBySha1(password: String) -> String {
return "ng"}
}
HashPasswordService.swift:41:7: Type 'HashPasswordServiceNgImpl'does not conform to protocol 'UsesReadConfigService'
実行前 DI漏 検出!
依存 増 効果的!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 16 / 21
DI 漏次 DI 忘
class HashPasswordServiceNgImpl: HashPasswordService {func hashBySha1(password: String) -> String {
return "ng"}
}
HashPasswordService.swift:41:7: Type 'HashPasswordServiceNgImpl'does not conform to protocol 'UsesReadConfigService'
実行前 DI漏 検出!
依存 増 効果的!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 16 / 21
DI 漏次 DI 忘
class HashPasswordServiceNgImpl: HashPasswordService {func hashBySha1(password: String) -> String {
return "ng"}
}
HashPasswordService.swift:41:7: Type 'HashPasswordServiceNgImpl'does not conform to protocol 'UsesReadConfigService'
実行前 DI漏 検出!
依存 増 効果的!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 16 / 21
DI 漏次 DI 忘
class HashPasswordServiceNgImpl: HashPasswordService {func hashBySha1(password: String) -> String {
return "ng"}
}
HashPasswordService.swift:41:7: Type 'HashPasswordServiceNgImpl'does not conform to protocol 'UsesReadConfigService'
実行前 DI漏 検出!
依存 増 効果的!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 16 / 21
DI 漏次 依存 増 、DI漏
protocol CreateUserService:UsesSessionService,UsesUserRepository,UsesClock,UsesApplicationLogger,UsesUserConfig,UsesRandomGenerator {
func create(user: User) -> Future<Session>}
大 起動 大変!
起動 DI漏 検出 便利!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 17 / 21
DI 漏次 依存 増 、DI漏
protocol CreateUserService:UsesSessionService,UsesUserRepository,UsesClock,UsesApplicationLogger,UsesUserConfig,UsesRandomGenerator {
func create(user: User) -> Future<Session>}
大 起動 大変!
起動 DI漏 検出 便利!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 17 / 21
DI 漏次 依存 増 、DI漏
protocol CreateUserService:UsesSessionService,UsesUserRepository,UsesClock,UsesApplicationLogger,UsesUserConfig,UsesRandomGenerator {
func create(user: User) -> Future<Session>}
大 起動 大変!
起動 DI漏 検出 便利!
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 17 / 21
Minimal Cake Pattern
静的 、DI漏 検出
DI 特別 必要普通 Cake Pattern 比
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 18 / 21
Minimal Cake Pattern静的 、DI漏 検出
DI 特別 必要普通 Cake Pattern 比
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 18 / 21
Minimal Cake Pattern静的 、DI漏 検出
DI 特別 必要
普通 Cake Pattern 比
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 18 / 21
Minimal Cake Pattern静的 、DI漏 検出
DI 特別 必要普通 Cake Pattern 比
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 18 / 21
目次
1 自己紹介
2 Dependency Injection ?
3 DI
4 Swift 代表的 DI手法
5 Example設定 読 込 機能
化 機能
DI 漏
6
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 19 / 21
参考文献
[1] 結城清太郎.Scala 最適 dependency injection 方法 考察~ 生産性 高 ~,2015.
[2] 人間.Dependency injection in swift 2.x, 2016.
[3] 吉村優.Swift minimal cake pattern, 2016.
[4] Takashi Tayama.Minimal cake pattern 作法, 2015.
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 20 / 21
Thank you for listening!
Any question?
吉村優 (@_yyu_) Minimal Cake Pattern in Swift September 16, 2016 21 / 21