【iOS】设计模式

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 2015/04/22更新:最新更新到Xcode6.3和Swfit 1.2。 更新日志:这个教程由Vicent Ng...
    木易林1阅读 337评论 0 0
  • 小白程序员只能看懂源代码,而大神程序员能看懂文档。 设计模式:为解决特定场景的问题而定制的解决方案。设计原则:构建...
    印林泉阅读 895评论 0 8
  • 做 iOS开发也好几年了,记得自己刚入行时,对iOS 开发模式也是一知半解,后面项目做多了,以及看一些优秀书籍之后...
    XcqRomance阅读 5,066评论 3 32
  • 你可以下载the project source from the end of part 1与我们共同来探索 这是...
    木易林1阅读 455评论 0 0
  • Cocoa 中常见的设计模式: 创建型 (Creational):单例模式 (Singleton) 结构型 (St...
    S大偉阅读 223评论 0 0