单接口,多类 的架构设计

2015-09-06 04:06:49 +08:00
 cralison

信息胜于事实

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

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

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

2120 次点击
所在节点    iDev
2 条回复
sablib
2015-09-06 14:07:41 +08:00
什么鬼,这种东西有一个函数接受一个 key 作为参数去执行操作不就可以了,为什么要写那么多函数。。
wzg1015
2015-09-28 16:32:15 +08:00
这么多重复代码,你写着不难受,我看着还难受呢.变与不变都没分清

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/218476

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX