【iOS】设计模式

作者: 清無 | 来源:发表于2017-08-10 11:23 被阅读18次

1。建造型模式Creational

  • 单例模式Singleton

2。结构型模式Structural

  • MVC

MVC
  • 装饰器Decorator

Extensions and Delegation.

  • 适配器Adapter

  • 立面Facade

Facade

3。行为型模式Behavioral

  • Observer

    • KVO

    • Notification

  • Memento 状态记忆

The memento pattern captures and externalizes an object's internal state.

Storyboard设置VC的Restoration ID
  • 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

相关文章

网友评论

    本文标题:【iOS】设计模式

    本文链接:https://www.haomeiwen.com/subject/kbtqrxtx.html