V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
iOS 开发实用技术导航
NSHipster 中文版
http://nshipster.cn/
cocos2d 开源 2D 游戏引擎
http://www.cocos2d-iphone.org/
CocoaPods
http://cocoapods.org/
Google Analytics for Mobile 统计解决方案
http://code.google.com/mobile/analytics/
WWDC
https://developer.apple.com/wwdc/
Design Guides and Resources
https://developer.apple.com/design/
Transcripts of WWDC sessions
http://asciiwwdc.com
Cocoa with Love
http://cocoawithlove.com/
Cocoa Dev Central
http://cocoadevcentral.com/
NSHipster
http://nshipster.com/
Style Guides
Google Objective-C Style Guide
NYTimes Objective-C Style Guide
Useful Tools and Services
Charles Web Debugging Proxy
Smore
davidlau
V2EX  ›  iDev

当 Controller 膨胀至上千行,混杂着业务逻辑和 UI 渲染逻辑时,如何重构?

  •  
  •   davidlau ·
    liuslevis · 2015-09-18 11:34:06 +08:00 · 3793 次点击
    这是一个创建于 3147 天前的主题,其中的信息可能已经有所发展或是发生改变。
    用什么设计模式 /思路?
    23 条回复    2015-09-22 11:20:03 +08:00
    zhangchioulin
        1
    zhangchioulin  
       2015-09-18 11:41:47 +08:00   ❤️ 1
    一步一步来呗,先不创建更多的文件,先把方法抽出,一个方法做一件事情
    然后抽取模型
    然后再根据方法抽出 View
    view 还能重构成,设置这个 view 控件的位置的一个 model 和生成 view 控件一个 Model
    ikaros
        2
    ikaros  
       2015-09-18 12:22:10 +08:00
    你题目已经回答了,把混杂的东西抽出来呗,单独写一个渲染的函数, 业务逻辑 抽出来做成 service 由 controller 调用
    ipconfiger
        3
    ipconfiger  
       2015-09-18 12:28:06 +08:00
    MVVM 啊
    StargazerWikiv
        4
    StargazerWikiv  
       2015-09-18 12:29:48 +08:00
    当初是怎么想的呢? 1k 多代码一个 controller 。。。
    shoaly
        5
    shoaly  
       2015-09-18 12:31:46 +08:00
    说白了 controller 就是一个文件夹, 这个文件夹下面现在太多东西了 , 先试着把文件分类, 然后建立更多的 controller , 将这些文件放过去.
    iEason
        6
    iEason  
       2015-09-18 12:35:32 +08:00
    数据层的全扔到 model 里去,视图该封装的封装。
    MVVM 也是不错的选择
    xi_lin
        7
    xi_lin  
       2015-09-18 12:54:10 +08:00
    页面元素特别多,事件绑定和响应处理特别多怎么破?大家的 View 处理 event 吗?我对 view 的理解一直是纯渲染,交互都给 controller 处理。然后在写复杂页面的时候 controller 就膨胀起来了。我能想到的只有分 category 处理。
    pi1ot
        8
    pi1ot  
       2015-09-18 12:55:53 +08:00   ❤️ 1
    辞职走人,下个接手的倒霉蛋会重写的
    zhigang1992
        9
    zhigang1992  
       2015-09-18 13:04:03 +08:00
    MVVM + Rx, iOS 的 DesignPatten 太多了,各种 delegate, KVO, target action, notification, blocks 等等等等。还是 Rx 来的舒服,代码好读好管理
    newtonisaac
        10
    newtonisaac  
       2015-09-18 13:06:44 +08:00
    辞职走人,下个接手的倒霉蛋会重写的
    yangjie6020
        11
    yangjie6020  
       2015-09-18 13:21:26 +08:00
    抽象 封装 剥离
    chairuosen
        12
    chairuosen  
       2015-09-18 13:25:04 +08:00
    @xi_lin 安利一下 vue
    chairuosen
        13
    chairuosen  
       2015-09-18 13:25:34 +08:00
    @xi_lin sorry 看错节点了哈哈哈哈
    xiaonengshou
        14
    xiaonengshou  
       2015-09-18 13:28:20 +08:00
    angular?
    xi_lin
        15
    xi_lin  
       2015-09-18 13:30:44 +08:00
    @zhigang1992 Rx 要放弃 7.0 可是公司还要支持,好难过。。
    xi_lin
        16
    xi_lin  
       2015-09-18 13:30:54 +08:00
    gsanidt
        17
    gsanidt  
       2015-09-18 13:31:38 +08:00
    推荐你看看这篇文章: https://www.objc.io/issues/1-view-controllers/
    damngood
        18
    damngood  
       2015-09-18 13:56:47 +08:00
    1. mvvm
    2. containment
    3. category || extension
    4. 常规功能封装
    cxq
        19
    cxq  
       2015-09-18 14:11:53 +08:00
    MVVM 用起来很舒服

    不过最近看别人的 android 项目代码,
    感觉他 Activity 数量和代码都非常少,看起来非常整洁
    主要是因为他们有 Fragment , 和 ListView 的 Adapter 独立出来了
    ibcker
        20
    ibcker  
       2015-09-20 04:50:42 +08:00
    mvvm
    ibcker
        21
    ibcker  
       2015-09-20 04:52:14 +08:00
    @cxq ListView 的 Adapter 在 iOS 同样适用,我写 tableview 一般就把 datasource 独立出来。
    cxq
        22
    cxq  
       2015-09-20 15:42:28 +08:00
    @ibcker 恩,我也是, 一发现变大了 就重构抽出来。另外最近还开始躲用自定义 View 抽出不少代码来。但是因为还在用 objective-c 所以看起来文件还是比他们多很多。
    Seakey
        23
    Seakey  
       2015-09-22 11:20:03 +08:00
    @xi_lin 建议将 Controller 分为业务逻辑控制器和视图控制器
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5290 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 07:34 · PVG 15:34 · LAX 00:34 · JFK 03:34
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.