ShannonChenCHN / iOS-App-Architecture

『iOS 应用架构研究』A journey of diving into iOS app architecture.
35 stars 9 forks source link

读『 iOS Architecture Patterns: Demystifying MVC, MVP, MVVM and VIPER 』 #4

Open ShannonChenCHN opened 6 years ago

ShannonChenCHN commented 6 years ago

读『 iOS Architecture Patterns: Demystifying MVC, MVP, MVVM and VIPER

一、引言

当我们面对架构时经常会遇到这样一些问题:

上面提到的都是些方向性的问题,也许你还有些其他更急切、更实际的问题要解决:

二、为什么要架构选型

1. 为什么要架构选型

因为如果你不考虑架构的话,很有可能就会面临这样一个问题,在一个包含各种逻辑的庞大的类中查找问题、进行调试,你会发现你根本就没法搞清楚这个类到底是干什么的,也难以把这个类当成一个独立的实体看待。实际情况很有可能是这样的:

让人感到困扰的是,Apple 的 MVC 并不能解决上面的这些问题。

2. 好的架构应该是什么样的

三、MV(X)

1. MV(X)

现在我们常见的几种架构设计模式有:

前三种架构模式的一个共同点,就是把各个实体分为 3 类:

这样划分的好处显而易见:

2. MVC

2.1 标准的 MVC

Traditional MVC doesn't seems to be applicable to modern iOS development.

2.2 Apple 的 MVC

理想

Controller 作为 View 和 Model 之间的中间者,这样 View 和 Model 就不需要知道对方了

现实

(图中的虚线代表什么?Model 和 Controller 之间的 notify 代表什么)

The interactions between the View and the Controller aren’t really testable with Unit Tests

回顾:对于小项目来说,也许你不需要考虑太多的架构问题,而且你也不希望有较高的维护成本,那么 Cocoa MVC 正好合适

Cocoa MVC is the best architectural pattern in terms of the speed of the development.

3. MVP

3.1 MVP 与 MVC 的比较:

MVP 和 MVC 最大的不同在于,MVC 的 View 和 Controller 严重耦合,而 MVP 的中间者 Presenter 跟 view controller 的生命周期没有半点关系,Presenter 中也没有 view 布局相关的代码,Presenter 只负责根据数据变化更新 View,以及处理 View 的事件

What if I told you, the UIViewController is the View.

3.2 MVP 的特点:

3.3 MVP 的问题(Router):

MVP in iOS means superb testability and a lot of code.

3.4 MVP 的变种:绑定和信号

4. MVVM:MV(X) 家族中最伟大的新生儿

4.1 MVVM 与 MVP 的比较

4.2 View Model 是什么

4.3 绑定

4.4 MVVM 的特点

The MVVM is very attractive, since it combines benefits of the aforementioned approaches, and, in addition, it doesn’t require extra code for the View updates due to the bindings on the View side. Nevertheless, testability is still on a good level.

5. VIPER

5.1 职能划分

5.2 VIPER 与 MV(X) 相比有什么不同

Proper way of doing routing is a challenge for the iOS applications, the MV(X) patterns simply don’t address this issue.

5.3 VIPER 的特点

四、结论

Make everything as simple as possible, but not simpler  — Albert Einstein