单接口,多类 的架构设计

清醒疯子 发布于 2015年09月06日
无人欣赏。

信息胜于事实

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

共3条回复
CALLPLA 回复于 2015年09月06日

网络抽了下,评论重复了, 不知道怎么删除评论。

CALLPLA 回复于 2015年09月06日

这样设计,调用起来(或者别人接手)还是非常复杂,非常难看。考虑以下2个修改场景,接手的人要如何入手?

  1. 调用某个view的时候,需要传不同的参数(比如从首页或者其他子模块进来,传递不同的入口信息)
  2. 更改某种统一风格的push

建议:

其实跳转逻辑和代码都是一样的。可以考虑做成统一的接口。设定一个基类ViewController,所有的子类ViewController统一继承父类的接口。我的做法是把每个圆按钮做成一个类,所有的按钮的调用接口是统一的,每个按钮的回调(点击响应)函数在首页的init里就确定好(或者根据接口传回的数据重新组织好顺序),甚至可以和View剥离,在model里确定。

吐槽: 函数、变量命名无法接受。

阅读起来非常吃力,还有拼写错误,比如第一个,就算用拼音,也拼写错误。

showALiLuXingViewController
清醒疯子 回复于 2015年09月06日

2楼 @CALLPLA

谢谢你的回复:)

请原谅我的无知m(_ _)m

1、如果想传任何参数,因为有属性在,应该是随便传的。

2、统一风格,可以调同一个对象实现,也可以继续同一父类实现。

3、通过多类实现,主要是不想依赖和实现集中在一个类上,目的是修改的时候只改部分不动整体。

登录 或者 注册
相关帖子