1。建造型模式Creational
-
单例模式Singleton
2。结构型模式Structural
-
MVC
-
装饰器Decorator
Extensions and Delegation.
-
适配器Adapter
-
立面Facade
3。行为型模式Behavioral
-
Observer
-
KVO
-
Notification
-
-
Memento 状态记忆
Storyboard设置VC的Restoration IDThe memento pattern captures and externalizes an object's internal state.
- AppDelegate.swift中加入方法:
func application(_ application: UIApplication, shouldSaveApplicationState coder: NSCoder) -> Bool {
return true
}
func application(_ application: UIApplication, shouldRestoreApplicationState coder: NSCoder) -> Bool {
return true
}
- ViewController中加入方法:
override func encodeRestorableState(with coder: NSCoder) {
coder.encode(currentAlbumIndex, forKey: Constants.IndexRestorationKey)
super.encodeRestorableState(with: coder)
}
override func decodeRestorableState(with coder: NSCoder) {
super.decodeRestorableState(with: coder)
currentAlbumIndex = coder.decodeInteger(forKey: Constants.IndexRestorationKey)
showDataForAlbum(at: currentAlbumIndex)
horizontalScrollerView.reload()
}
- Archiving and Serialization
链接
https://www.raywenderlich.com/160651/design-patterns-ios-using-swift-part-12
https://github.com/ochococo/Design-Patterns-In-Swift
网友评论