67 lines
1.7 KiB
Markdown
67 lines
1.7 KiB
Markdown
---
|
||
paths:
|
||
- "**/*.swift"
|
||
- "**/Package.swift"
|
||
---
|
||
# Swift 模式 (Patterns)
|
||
|
||
> 本檔案擴展了 [common/patterns.md](../common/patterns.md),包含 Swift 特定內容。
|
||
|
||
## 協定導向設計 (Protocol-Oriented Design)
|
||
|
||
定義精簡、焦點明確的協定。使用協定擴展 (Protocol extensions) 來提供共享的預設實作:
|
||
|
||
```swift
|
||
protocol Repository: Sendable {
|
||
associatedtype Item: Identifiable & Sendable
|
||
func find(by id: Item.ID) async throws -> Item?
|
||
func save(_ item: Item) async throws
|
||
}
|
||
```
|
||
|
||
## 值型別 (Value Types)
|
||
|
||
- 使用 Structs 作為資料傳輸物件 (DTOs) 與模型。
|
||
- 使用帶有關聯值的 Enums 來模擬相異的狀態:
|
||
|
||
```swift
|
||
enum LoadState<T: Sendable>: Sendable {
|
||
case idle
|
||
case loading
|
||
case loaded(T)
|
||
case failed(Error)
|
||
}
|
||
```
|
||
|
||
## Actor 模式
|
||
|
||
對於共享的可變狀態,使用 Actors 而非鎖 (Locks) 或派遣佇列 (Dispatch queues):
|
||
|
||
```swift
|
||
actor Cache<Key: Hashable & Sendable, Value: Sendable> {
|
||
private var storage: [Key: Value] = [:]
|
||
|
||
func get(_ key: Key) -> Value? { storage[key] }
|
||
func set(_ key: Key, value: Value) { storage[key] = value }
|
||
}
|
||
```
|
||
|
||
## 依賴注入 (Dependency Injection)
|
||
|
||
使用帶有預設參數的協定進行注入 — 生產環境使用預設值,測試環境則注入 Mock:
|
||
|
||
```swift
|
||
struct UserService {
|
||
private let repository: any UserRepository
|
||
|
||
init(repository: any UserRepository = DefaultUserRepository()) {
|
||
self.repository = repository
|
||
}
|
||
}
|
||
```
|
||
|
||
## 參考資源
|
||
|
||
參見技能 (Skill):`swift-actor-persistence`,獲取基於 Actor 的持久化模式。
|
||
參見技能 (Skill):`swift-protocol-di-testing`,獲取基於協定的 DI 與測試模式。
|