iOS架构:Proxy实现局部模块化(附Demo)

栏目: IOS · 发布时间: 7年前

内容简介:Proxy 设计模式:为其他对象提供一种代理以控制对这个对象的访问。在 iOS 开发中,Proxy 设计模式的体现更多的是以单个代理的方式,笔者为了优雅的达到更深层次的模块化,基于消息转发间接的实现多代理。喜欢看代码的可直接看 DEMO

一、写在前面

Proxy 设计模式:为其他对象提供一种代理以控制对这个对象的访问。

在 iOS 开发中,Proxy 设计模式的体现更多的是以单个代理的方式,笔者为了优雅的达到更深层次的模块化,基于消息转发间接的实现多代理。

喜欢看代码的可直接看 DEMO

Proxy 局部模块化 DEMO

二、更深层次模块化

在 iOS App 中,MVC 和 MVVM 是比较流行的架构模式,而当某个界面业务量达到一个程度过后,MVVM 甚至是 VIPER 模式都显得有些力不从心。

为了达到更高层次的模块化,往往会做其他方面的工作,比如将 UITableView 等代理方法的实现独立出,然而代理方法里面的逻辑太多会导致独立出来的类仍然臃肿;更细化一点的法子是在代理方法里面将具体实现分离出去,由其他类完成。

不过,这些方式都不够优雅。

所以,这就是本文的目的,提供一种更深层次的模块化的方案。

三、实际应用

Proxy设计模式,在笔者之前的框架中已经有了应用,实现该框架难点就是:利用方法重定向实现多接收者的消息转发。

详情可看这篇文章,文章中间部分有对消息转发流程的简述:

iOS解决方案:文本输入控制(献上框架)

本文就不讲解消息发送机制了,在 Demo 中有封装 —— YBProxyManager ,我们将利用它来做局部模块化。

在实际业务需求中,出场率很高的是 UITalbeViewUICollecitonView 等需要用大量代理方法配置的视图,当然这是苹果程序设计的惯例。当UI界面很复杂,业务逻辑相当多的时候,虽然把网络请求、数据处理、视图封装等都模块分离出去了,但是配置代理里面的逻辑太多,我们想要每一个类处理一部分代理方法。

Demo 以 UITableView 为例。

首先,创建实现 UITableView 代理的三个类:

@implementation TestTableViewDigitConfig- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {    return 20;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {    return 80;
}@end
@implementation TestTableViewClickConfig- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {    NSLog(@"click -- section:%ld, row:%ld", indexPath.section, indexPath.row);
}@end
@implementation TestTableViewCellConfig- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass(UITableViewCell.class)];    if (!cell) {
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass(UITableViewCell.class)];
    }
    cell.textLabel.text = [NSString stringWithFormat:@"第%ld行", indexPath.row];    return cell;
}@end

如代码所见,这里将 tableView 的代理用三个类来分别实现,然后在 UIViewController 里面只需要写这些代码:

@interface TestVC ()@property (nonatomic, strong) UITableView *tableView;@property (nonatomic, strong) YBProxyManager *proxyManager;@property (nonatomic, strong) TestTableViewDigitConfig *digitConfig;@property (nonatomic, strong) TestTableViewClickConfig *clickConfig;@property (nonatomic, strong) TestTableViewCellConfig *cellConfig;@end@implementation TestVC#pragma mark life cycle- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.tableView];
}#pragma mark getter- (UITableView *)tableView {    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:[UIScreen mainScreen].bounds style:UITableViewStylePlain];
        _tableView.tableFooterView = [UIView new];
        
        _digitConfig = [TestTableViewDigitConfig new];
        _clickConfig = [TestTableViewClickConfig new];
        _cellConfig = [TestTableViewCellConfig new];
        
        _proxyManager = [YBProxyManager new];
        [_proxyManager addTarget:_digitConfig];
        [_proxyManager addTarget:_clickConfig];
        [_proxyManager addTarget:_cellConfig];
        
        _tableView.delegate = (id
<uitableviewdelegate>
 )_proxyManager;
        _tableView.dataSource = (id
 <uitableviewdatasource>
  )_proxyManager;
    }    return _tableView;
}@end
 </uitableviewdatasource>
</uitableviewdelegate>

核心代码就是将 YBProxyManager 类的使用:

当你需要使用多个对象(target)来承接一些方法的实现,初始化 YBProxyManager 实例,将这些对象实例添加到

YBProxyManager 实例中(addTarget),最后将 YBProxyManager 实例作为这些方法的第一承接者。剩下的方法分发工作就交给该类了。

代码请看 DEMO,不复杂。

Proxy 局部模块化 DEMO

笔者提示:这只是一个玩儿法,可能会让架构更加复杂,所以重在理解思想,谨慎使用。


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Algorithms Unlocked

Algorithms Unlocked

Thomas H. Cormen / The MIT Press / 2013-3-1 / USD 25.00

Have you ever wondered how your GPS can find the fastest way to your destination, selecting one route from seemingly countless possibilities in mere seconds? How your credit card account number is pro......一起来看看 《Algorithms Unlocked》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

XML、JSON 在线转换
XML、JSON 在线转换

在线XML、JSON转换工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具