漫談 iOS 架構:MVC / MVVM / VIPER 與 Redux - Nelson === {%hackmd FGRWt1UuTFKk0wSbGgg-SA %} > 請從這裡開始 ## YouTube {%youtube qDyb6AkX0s0%} ## Slide [{%slideshare nelson0805/common-ios-architecture-from-mvc-to-viper-with-redux %}](https://) [文字稿在這裡](https://chiahsien.github.io/post/common-ios-architecture-from-mvc-to-viper-with-redux/) [TOC] ## Why this topic? 通常是接手別人的 code 團隊成員變多之後要如何應對與維護 from startup found buggy code all articles start with "mvc sucks" without any reason ## From MVC to VIPER * Series of refactoring * Pick one that suit your team * 不一定哪一種比較好 ### MVC ![](https://i.imgur.com/qEp9hZy.jpg) Model - Data structdfive view controller con<font color=red>**troll**</font>er ### 如何解決 MASSIVE VIEW CONTROLLER #### MVP and MVVM ![](https://i.imgur.com/cOjDuf8.jpg) Presenter / ViewModel - API - business logic - data conversion - data source 重點在於將不屬於 viewController 的事情拆解出來 可以是 presenter 的 MVP or viewModel 的 MVVM MVP ≈ MVVM MVVM: 利用 data binding(ex: RxSwift, combine)更新 model MVP: 利用 block, delegate 更新 model Weakness: - Hard to reuse - Lack of flexibility - Hard to cowork - conflict in storyboard when coworking ### UI Navigation Solution ~~ Coordinator ![](https://i.imgur.com/80RBvEk.jpg) * coordinator: 可以想成一組 module / feature 的入口 * VC 間應該互相獨立,靠 coordinator 做聯繫 * Coordinator * Create and setup VCs * Manage VC's flow * Manage child cooridinators * 只負責 UI Navigation ![](https://i.imgur.com/FhTzFzC.jpg) ### VIPER ![](https://i.imgur.com/YenSlng.jpg) > every project will end up as viper, maybe Model => Entity View Presenter Coordinator=> Router Manager/Service => Interactor 就是 VIP MVC -> VIPER:就是不停 Refactoring。 ## State Management 上吧 singleton (GG),但如果不想用這種方式的話呢? ## Redux ![](https://i.imgur.com/NEVf3UO.jpg) [ReSwift](https://github.com/ReSwift/ReSwift) just copy from front-End, almost the same 🤬🤬🤬🤬🤬 * A state management framework * Single source of truth * Single place to modify state (只有一個(人)可以修改) * Unidirctional data flow (單向資料流) Reducer: 判斷 state 的地方 redux in viper: `interactor + entity` ## Summary * A series of refactoring * Find a pattern that suit your team * Both VM and Presenter are great * 👍**Recommend Coordinator for beginner** (很推薦,效果好,技術門檻低,可以融入其他架構) * Manage state with care ## Others 「因為聽了 weak self 才知道這場」 +1, -1 > 本日經典名言:「資深工程師的生活就是這麽樸實無華,且枯燥。」 ![](https://i.imgur.com/jhfw1xQ.png) > Refactoring is usually motivated by noticing a code smell. - wiki [Code_refactoring](https://en.wikipedia.org/wiki/Code_refactoring) > 聽完覺得是talk show 的 +1 -> 1 ###### tags:`iPlayground2019`