Loading...
Loading...
Compare original and translation side by side
| Complexity | Pattern | Use When |
|---|---|---|
| Simple | MV | Single screen, local state only |
| Medium | MVVM | 2-5 screens, business logic, network calls |
| Complex | TCA | State machines, side effects, complex flows |
| Enterprise | Clean | Multiple teams, maximum modularity |
| 复杂度 | 模式 | 适用场景 |
|---|---|---|
| 简单 | MV | 单页面、仅本地状态 |
| 中等 | MVVM | 2-5个页面、包含业务逻辑、网络请求 |
| 复杂 | TCA | 状态机、副作用、复杂流程 |
| 企业级 | Clean | 多团队协作、高模块化需求 |
1. Single screen + local state only?
→ MV (SwiftUI View + @State)
2. Business logic or shared state?
→ MVVM
3. Complex state transitions?
→ TCA
4. Multi-team, high modularity?
→ Clean Architecture1. 仅单页面 + 本地状态?
→ MV (SwiftUI View + @State)
2. 包含业务逻辑或共享状态?
→ MVVM
3. 存在复杂状态转换?
→ TCA
4. 多团队协作、高模块化需求?
→ Clean Architecture@Observable@State private var viewModel@MainActor@Published@Observableprotocol AuthServiceProtocol {
func login(_ email: String, _ password: String) async throws -> User
}
@Observable
final class LoginViewModel {
private let authService: AuthServiceProtocol
var email = ""
var password = ""
var isLoading = false
init(authService: AuthServiceProtocol = AuthService()) {
self.authService = authService
}
@MainActor
func login() async {
isLoading = true
defer { isLoading = false }
try? await authService.login(email, password)
}
}
struct LoginView: View {
@State private var viewModel = LoginViewModel()
var body: some View {
Form {
TextField("Email", text: $viewModel.email)
SecureField("Password", text: $viewModel.password)
Button("Login") { Task { await viewModel.login() } }
.disabled(viewModel.isLoading)
}
}
}@Observable@State private var viewModel@MainActor@Published@Observableprotocol AuthServiceProtocol {
func login(_ email: String, _ password: String) async throws -> User
}
@Observable
final class LoginViewModel {
private let authService: AuthServiceProtocol
var email = ""
var password = ""
var isLoading = false
init(authService: AuthServiceProtocol = AuthService()) {
self.authService = authService
}
@MainActor
func login() async {
isLoading = true
defer { isLoading = false }
try? await authService.login(email, password)
}
}
struct LoginView: View {
@State private var viewModel = LoginViewModel()
var body: some View {
Form {
TextField("Email", text: $viewModel.email)
SecureField("Password", text: $viewModel.password)
Button("Login") { Task { await viewModel.login() } }
.disabled(viewModel.isLoading)
}
}
}ReducerTestStoreReducerTestStorevip-clean-architecturevip-clean-architecturereferences/mvvm-patterns.mdreferences/tca-guide.mdreferences/clean-architecture.mdreferences/mvvm-patterns.mdreferences/tca-guide.mdreferences/clean-architecture.md