单接口,多类 的架构设计 - V2EX
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 2689 次点击
    这是一个创建于 3766 天前的主题,其中的信息可能已经有所发展或是发生改变。

    信息胜于事实

    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 )howTianMaoChaoShiViewController { [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     Solana     2602 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 27ms UTC 06:27 PVG 14:27 LAX 22:27 JFK 01:27
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86