Comments (6)
参考资料
- Model-View-Controller - Cocoa Core Competencies
- Model-View-Controller - Concepts in Objective-C Programming
- Model object - Apple Developer Documentation
- Controller object - Apple Developer Documentation
- Model-View-Controller (MVC) in iOS: A Modern Approach - Ray Wenderlich
- MVCNetworking - Guides and Sample Code: A sample that shows how to create a network application using the Model-View-Controller design pattern.
- Lecture 2: MVC; iOS, Xcode and Swift Demonstration - Stanford
from ios-app-architecture.
读『Model-View-Controller - Cocoa Core Competencies』
一、概述
- Model-View-Controller (MVC) 设计模式将对象分成三类:model、 view、 或者 controller
- MVC 不仅仅定义了对象的角色,而且还对象之间通信的方式
- 一般我们把属于 MVC 其中一种角色的那些类称为层(layer),比如 model 层
二、 Model 层
- 职责
- 封装数据
- 处理数据相关的逻辑
- 与 View 层没有直接关联
- 通信
- View 层接收到用户操作事件后,会通知 Controller 来创建或者修改数据,实际上也就是创建或者更新 model 对象
- 当一个 model 对象发生变化时(比如,网络请求后返回新的数据),它会告知 Controller 层,然后 Controller 就会更新对应的 View
三、View 层
- 职责
- 绘制 UI 界面(展示 model 层的数据)
- 响应用户操作(修改 model 层的数据)
- 与 Model 层是解耦的
- 因为 View 一般会被重用以及重新配置,所以各应用间的 view 对象都具有一致性。UIKit 和 AppKit框架都提供了很多可以直接使用的 View 类,比如 UIButton、UILabel
- 通信
- View 只能通过 Controller 得知 Model 中的数据更新
- View 接收到用户事件(比如在 UITextField 中输入文本)后通过 Controller 去更新 Model 层的数据
四、Controller 层
- 职责
- 作为 View 层和 Model 层之间的中间人
- 作为 View 层和 Model 层通信的桥梁:View 收到事件后通过 Controller 更新 Model,同样地,Model 发生变化后,通过 Controller 刷新 View 展示
- 处理应用中的设置、协调任务,管理其他对象的声明周期
- 通信
- Controller 会处理 View 接收到的事件,更新 Model 层的数据
- 当 Model 层发生变化时,Controller 会将最新的 Model 数据同步到 View 层进行展示
from ios-app-architecture.
读『Model object - Apple Developer Documentation』
1. 概述
- 一个 model 对象包含应用的数据,以及提供数据存取的接口和实现数据处理的逻辑。
- modle 对象扮演着 MVC 的三个角色之一
- 所有从文件读取出来或者从网络获取到的数据都应该交给 model 管理
- 因为 model 对象通常代表某个专属领域的知识,所以在解决某个专属领域的问题时,就会复用相应的 model 对象。比如
UIAlertAction
就是用来封装UIAlertController
上的按钮对应的信息的。 - model 对象应该跟 view 对象没有直接关联,也就是说,model 对象不关心用户交互和 UI 展示
2. 一个精心设计的 Model 类
一个 model 类通常继承于 NSObject 或者 NSManagedObject(如果你使用了 Core Data 的话)。
设计一个 Model 类,通常要考虑以下几个方面:
-
实例变量(Instance variables):我们可以通过声明实例变量来表征应用的数据。
- 实例变量可以是对象,也可以是标量值(也就是 C 基本数据类型),还可以是结构体(比如 NSRange)
- 在选择变量的类型时,需要权衡一下到底是用对象类型还是非对象类型,另外,对于对象类型的变量,还需要考虑可变性(mutability)的问题。
-
存取方法和属性声明(Accessor methods and declared properties):
- 存取方法和属性作为访问对象数据的通道,保证了数据的封装性。
- 存取方法(也就是 getter 和 setter)用来读取和设置实例变量的值
- 属性其实是 Objective-C 在语言层面提供的便利,它会通过运行时自动合成相应实例变量的存取方法
- 存取方法和属性的另外一个重要的作用就是管理对象的内存,所以在实现 getter 和 setter 方法时会有些需要注意的地方
- 存取方法和属性作为访问对象数据的通道,保证了数据的封装性。
-
KVC(Key-value coding): KVC 是一种能够让访问者通过把属性名作为 key 来访问属性的机制
- (void)setValue:(nullable id)value forKey:(NSString *)key; - (nullable id)valueForKey:(NSString *)key;
-
初始化和释放(Initialization and deallocation):
- 初始化:大多数情况下,model 类会在初始化方法中对其实例变量的值进行初始化
- 释放:在 model 类的
dealloc
方法被调用时,还应该保证其实例变量也被正确地释放了
-
对象编解码(Object encoding):如果你希望一个 model 类能够被归解档(to be archived or unarchived),你就要实现
NSCoding
协议,对其属性/实例变量进行编码解码(encode and decode) -
对象拷贝(Object copying):如果你希望你的 model 对象能被使用方复制(copy),那么你就需要为这个 model 类实现
NSCopying
协议或者NSMutableCopying
协议
from ios-app-architecture.
读『Controller object - Apple Developer Documentation』
controller 的职责
- 通常扮演着 view 和 model 之间的中间人和协调者的角色。
- 在 MVC 中,controller 会处理 view 中传递过来的事件,同时更新 model 中的数据。
- 当 model 发生变化时(比如用户打开一个本地文档),controller 会将新数据同步到对应的 view 上进行展示。
- 处理应用中的设置、协调任务,管理其他对象的声明周期
Cocoa 框架提供了三种类型的 controller:
- coordinating controllers
- view controllers (on iOS)
- mediating controllers (on OS X)
iOS 中通常只有 view controllers
from ios-app-architecture.
读『Model-View-Controller - Concepts in Objective-C Programming』
from ios-app-architecture.
读『 Model-View-Controller (MVC) in iOS: A Modern Approach - Ray Wenderlich』
from ios-app-architecture.
Related Issues (13)
- 读『杂谈: MVC/MVP/MVVM』 HOT 1
- 读 『如何写好一个UITableView』 HOT 1
- 读『iOS应用架构谈』系列 HOT 2
- 读『 iOS Architecture Patterns: Demystifying MVC, MVP, MVVM and VIPER 』 HOT 1
- 如何成为一名合格的、优秀的架构师? HOT 1
- 浅谈 iOS 应用架构 HOT 1
- MVVM HOT 1
- 读『优秀的 iOS 应用架构:MVVM、MVC、VIPER,孰优孰劣?』 HOT 1
- 设计模式(一) HOT 5
- 读『Much ado about iOS app architecture』
- 如何设计 iOS 应用网络层? HOT 1
- API 设计原则 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from ios-app-architecture.