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
cralison
V2EX  ›  iDev

单接口,多类 的架构设计

  •  
  •   cralison · 2015-09-06 04:06:49 +08:00 · 2117 次点击
    这是一个创建于 3367 天前的主题,其中的信息可能已经有所发展或是发生改变。

    信息胜于事实

    APP 架构设计有三种方式,我们以这个页面中间圆形按钮的页面跳转为例:

    alt text

    不管是什么设计,我们都会有一堆目标 ViewController 类:

    alt text

    1 、最原始的设计:

    ViewController.m

    #import "ALiLuXingViewController.h"
    #import "ChongZhiViewController.h"
    #import "FengLeiViewController.h"
    #import "JuHuaSuanViewController.h"
    #import "KouBeiWaiMaiViewController.h"
    #import "LingJinBiViewController.h"
    #import "TaoShengHuoViewController.h"
    #import "TianMaoViewController.h"
    #import "TianMaoGuoZhiViewController.h"
    #import "TianMaoChaoShiViewController.h"
    
    -(void )showALiLuXingViewController
    {
        ALiLuXingViewController *aLiLuXingViewController = [[ALiLuXingViewController alloc]init];
        [self.navigationController pushViewController:aLiLuXingViewController animated:YES];
    }
    
    -(void )showChongZhiViewController
    {
        ChongZhiViewController *chongZhiViewController = [[ChongZhiViewController alloc]init];
        [self.navigationController pushViewController:chongZhiViewController animated:YES];
    }
    
    -(void )showFengLeiViewController
    {
        FengLeiViewController *fengLeiViewController = [[FengLeiViewController alloc]init];
        [self.navigationController pushViewController:fengLeiViewController animated:YES];
    }
    
    -(void )showJuHuaSuanViewController
    {
        JuHuaSuanViewController *juHuaSuanViewController = [[JuHuaSuanViewController alloc]init];
        [self.navigationController pushViewController:juHuaSuanViewController animated:YES];
    }
    
    -(void )showKouBeiWaiMaiViewController
    {
        KouBeiWaiMaiViewController *kouBeiWaiMaiViewController = [[KouBeiWaiMaiViewController alloc]init];
        [self.navigationController pushViewController:kouBeiWaiMaiViewController animated:YES];
    }
    
    -(void )showLingJinBiViewController
    {
        LingJinBiViewController *lingJinBiViewController = [[LingJinBiViewController alloc]init];
        [self.navigationController pushViewController:lingJinBiViewController animated:YES];
    }
    
    -(void )showTaoShengHuoViewController
    {
        TaoShengHuoViewController *taoShengHuoViewController = [[TaoShengHuoViewController alloc]init];
        [self.navigationController pushViewController:taoShengHuoViewController animated:YES];
    }
    
    -(void )showTianMaoViewController
    {
        TianMaoViewController *tianMaoViewController = [[TianMaoViewController alloc]init];
        [self.navigationController pushViewController:tianMaoViewController animated:YES];
    }
    
    -(void )showTianMaoGuoZhiViewController
    {
        TianMaoGuoZhiViewController *tianMaoGuoZhiViewController = [[TianMaoGuoZhiViewController alloc]init];
        [self.navigationController pushViewController:tianMaoGuoZhiViewController animated:YES];
    }
    
    -(void )showTianMaoChaoShiViewController
    {
        TianMaoChaoShiViewController *tianMaoChaoShiViewController = [[TianMaoChaoShiViewController alloc]init];
        [self.navigationController pushViewController:tianMaoChaoShiViewController animated:YES];
    }
    

    2 、单类,多接口 设计:

    RouteInterface.h

    #ifndef ViewControllerSplit_RouteInterface_h
    #define ViewControllerSplit_RouteInterface_h
    @protocol RouteInterface <NSObject>
    
    @optional
    -(void )showALiLuXingViewController;
    -(void )showChongZhiViewController;
    -(void )showFengLeiViewController;
    -(void )showJuHuaSuanViewController;
    -(void )showKouBeiWaiMaiViewController;
    -(void )showLingJinBiViewController;
    -(void )showTaoShengHuoViewController;
    -(void )showTianMaoViewController;
    -(void )showTianMaoGuoZhiViewController;
    -(void )showTianMaoChaoShiViewController;
    @end
    #endif
    

    ViewController.h

    #import <UIKit/UIKit.h>
    #import "RouteInterface.h"
    
    @interface ViewController : UIViewController
    
    @property (nonatomic, weak ) id<RouteInterface> routeHandle;
    @end
    ViewController.m
    
    -(void )showALiLuXingViewController
    {
        [self.routeHandle showALiLuXingViewController];
    }
    
    -(void )showChongZhiViewController
    {
        [self.routeHandle showChongZhiViewController];
    }
    
    -(void )showFengLeiViewController
    {
        [self.routeHandle showFengLeiViewController];
    }
    
    -(void )showJuHuaSuanViewController
    {
        [self.routeHandle showJuHuaSuanViewController];
    }
    
    -(void )showKouBeiWaiMaiViewController
    {
        [self.routeHandle showKouBeiWaiMaiViewController];
    }
    
    -(void )showLingJinBiViewController
    {
        [self.routeHandle showLingJinBiViewController];
    }
    
    -(void )showTaoShengHuoViewController
    {
        [self.routeHandle showTaoShengHuoViewController];
    }
    
    -(void )showTianMaoViewController
    {
        [self.routeHandle showTianMaoViewController];
    }
    
    -(void )showTianMaoGuoZhiViewController
    {
        [self.routeHandle showTianMaoGuoZhiViewController];
    }
    
    -(void )showTianMaoChaoShiViewController
    {
        [self.routeHandle showTianMaoChaoShiViewController];
    }
    

    3 、单接口,多类 设计:

    RouteInterface.h

    #ifndef ViewControllerSplit_RouteInterface_h
    #define ViewControllerSplit_RouteInterface_h
    @protocol RouteInterface <NSObject>
    
    @optional
    -(void )showViewController;
    @end
    #endif
    

    ViewController.h

    #import <UIKit/UIKit.h>
    #import "RouteInterface.h"
    
    @interface ViewController : UIViewController
    
    @property (nonatomic, weak ) id<RouteInterface> aLiLuXingRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> chongZhiRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> fengLeiRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> juHuaSuanRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> kouBeiWaiMaiRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> lingJinBiRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> taoShengHuoRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> tianMaoRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> tianMaoGuoZhiRouteHandle;
    @property (nonatomic, weak ) id<RouteInterface> tianMaoChaoShiRouteHandle;
    @end
    

    ViewController.m

    -(void )showALiLuXingViewController
    {
        [self.aLiLuXingRouteHandle showViewController];
    }
    
    -(void )showChongZhiViewController
    {
        [self.chongZhiRouteHandle showViewController];
    }
    
    -(void )showFengLeiViewController
    {
        [self.fengLeiRouteHandle showViewController];
    }
    
    -(void )showJuHuaSuanViewController
    {
        [self.juHuaSuanRouteHandle showViewController];
    }
    
    -(void )showKouBeiWaiMaiViewController
    {
        [self.kouBeiWaiMaiRouteHandle showViewController];
    }
    
    -(void )showLingJinBiViewController
    {
        [self.lingJinBiRouteHandle showViewController];
    }
    
    -(void )showTaoShengHuoViewController
    {
        [self.taoShengHuoRouteHandle showViewController];
    }
    
    -(void )showTianMaoViewController
    {
        [self.tianMaoRouteHandle showViewController];
    }
    
    -(void )showTianMaoGuoZhiViewController
    {
        [self.tianMaoGuoZhiRouteHandle showViewController];
    }
    
    -(void )showTianMaoChaoShiViewController
    {
        [self.tianMaoChaoShiRouteHandle showViewController];
    }
    

    后记(下面以聊家常为主,没时间没兴趣的朋友请直接忽略):

    目前,我的行为准则只剩两个: 1 、减小焦点; 2 、叠加信息。据说,与王阳明的“心学”类似。

    其中,叠加信息是根本,减小焦点是技巧。

    任何事情的困难,都是信息不足造成的。

    包括所谓的行动力不足,也可以通过增加信息来解决。比如,不断增加“行动有巨大好处,不行动将立马毁灭”这样的信息。

    人对人的控制,也是从增加单方面信息下手的。只要不停地禁止其它声音,只允许单方面发声,即使把更多的自由发放给人们,人们还是会无可避免地按照控制者指定的方向前进。

    奴役,从来都是通过信息管制来最终实现的。

    而个人想自救,也只有从信息获取这里下手,才能根本解决。

    但是,现在是一个信息爆炸的时代,全球每天有 36 万 7 千本书出版。你是不可能全方面地获取所有信息的。
    为了从世界巨量信息中拯救自己,我们只能采取收缩策略:把自己的关注点尽可能地减少缩小。最好是只关注一个这个世界上除了你,没有人会关注的点上。

    然后,收集所有关于这个足够小的点的信息,让这个单点全部信息塞进大脑,反复啄磨。如果在这个足够小的点上,你能更好地重组现有的信息,甚至创造新信息,就是对这个世界巨大的贡献。

    分享一个 TED:
    http://www.miaopai.com/show/OXEFXdUIU90yduUazs1gBA__.htm

    2 条回复    2015-09-28 16:32:15 +08:00
    sablib
        1
    sablib  
       2015-09-06 14:07:41 +08:00
    什么鬼,这种东西有一个函数接受一个 key 作为参数去执行操作不就可以了,为什么要写那么多函数。。
    wzg1015
        2
    wzg1015  
       2015-09-28 16:32:15 +08:00
    这么多重复代码,你写着不难受,我看着还难受呢.变与不变都没分清
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1053 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 23ms · UTC 18:36 · PVG 02:36 · LAX 10:36 · JFK 13:36
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.