• IOS的MVC和MVVM模式简明介绍


    iOS中的MVC(Model-View-Controller)将软件系统分为Model、View、Controller三部分,结构图如下:

    Model: 你的应用本质上是什么(但不是它的展示方式)
    Controller:你的Model怎样展示给用户(UI逻辑)
    View:用户看到的,被Controller操纵着的
     
    Controller可以直接访问Model,也可以直接控制View。
    但Model和View不能互相通信。
     
    View可以通过action-target的方式访问Controller,比如我们在StoryBoard中拖UIButton到代码中所创建的@IBAction,当按钮被点击时,View就会传递该信息给Controller。
    有时候Controller需要实时监控View的状态,这时Controller会通过protocol将其自身设为View的delegate,这样当View will change、should change、did change 的时候Controller也会接到相应通知。
    View不存储数据,但View可以通过协议获取Controller而不是Model中的数据用来展示。
    Controller整理Model中的数据用于给View展示。
     
    Model不能直接与Controller通讯,因为Model是独立于UI存在的。
    但当Model发生改变想通知Controller时可使用广播机制,在iOS中有NSNotification和KVO(Key-value observing)可供使用。
    NSNotification:
    1  let center = NSNotificationCenter.defaultCenter()
    2         
    3         center.addObserverForName(UIContentSizeCategoryDidChangeNotification,
    4             object: UIApplication.sharedApplication(),
    5             queue: NSOperationQueue.mainQueue())
    6             { notification in
    7                 let c = notification.userInfo?[UIContentSizeCategoryNewValueKey]
    8         }

    KVO:

    1   webView.addObserver(self, forKeyPath: "estimatedProgress", options: .New, context: nil)
    2 
    3      override func observeValueForKeyPath(keyPath: String?, ofObject object: AnyObject?, change: [NSObject : AnyObject]?, context: UnsafeMutablePointer<Void>) {
    4              if (keyPath == "estimatedProgress") {
    5                  progressView.hidden = webView.estimatedProgress == 1
    6                  progressView.setProgress(Float(webView.estimatedProgress), animated: true)
    7              }
    8          }
    MVP模式从经典的MVC模式演变而来,将Controller替换成Presenter,依据MVP百度百科中的解释,MVP的优点相比较于MVC是完全分离Model与View,Model与View的信息传递只能通过Controller/Presenter,我查阅资料发现在其他平台上的MVC模式View与Model能否直接通讯有着不同的说法,但在iOS开发中,Apple是这么说的。在MVC下,所有的对象被归类为一个model,一个view,或一个controller。Model持有数据,View显示与用户交互的界面,而View Controller调解Model和View之间的交互,在iOS开发中我按照Model与View无法相互通讯来理解。
     
    MVVM(Model View View-Model)
    上图展示了MVVM与MVC的差别。
     
    在MVC模式的iOS开发中,Controller承担了太多的代码,包含着我们的视图处理逻辑和业务逻辑。
    在MVVM中,我们将视图处理逻辑从C中剥离出来给V,剩下的业务逻辑部分被称做View-Model。
    使用MVVM模式的iOS应用的可测试性要好于MVC,因为ViewModel中并不包含对View的更新,相比于MVC,减轻了Controller的负担,使功能划分更加合理。
     
     
  • 相关阅读:
    Oracle Golden Gate
    DNS
    RMAN 管理
    黄伟-RAC生产库现场调整本分策略实战
    Linux下RAID技术
    OCP之黄伟 2
    About NULL Value
    OCP之黄伟
    Table
    Perl的简单变量
  • 原文地址:https://www.cnblogs.com/cy568searchx/p/5955763.html
Copyright © 2020-2023  润新知