iPhone开发重构:提取公用的方法以清理重复代码 【转】

    无论在iPhone开发还是学习的过程中都会看到一些不是很理想的代码,不可否认自己也在不断“贡献”着这类代码。面对一些代码的“坏味道”,重构显然是 个有效的解决途径。《iPhone开发重构》系列就想总结和补充iPhone开发中经历的一些重构,其间可能会引用一些开源以及实际项目的代码,本着对技 术的探求,冒昧之处还请作者多多见谅。

   代码重复是一个比较明显的“坏味道”,提取公用的方法就是解决的途径之一。iPhone开发中,使用UITableView的时候就有如下一段“经典”的 模板代码,因为这是项目模板自动生成的,所以很多人就自然接受了。但随着越来越多地通过copy&paste在一个项目中使用这段代码,大家是否 有些采取行动的压力呢?好吧,我们就从这“动刀”吧!

重构前:

- (UITableViewCell *)tableView:(UITableView *)tv cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *CellIdentifier = @"wikiHowCell";
    UITableViewCell *cell = (WHTableViewCell *)[tv dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[WHTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

   对此我们可以提取个公共的方法,并放置在一个适当的地方。UITableViewCell的Category应该是一个比较好的去处。重构后提取的方法以及实际调用的代码如下:

重构后:

@implementation UITableViewCell(Cache)

+ (id)dequeOrCreateInTable:(UITableView*)tableView withId: (NSString*)reuseId andStyle:(UITableViewCellStyle)style {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuseId];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:style reuseIdentifier:reuseId] autorelease];
    }
    return cell;
}

@end

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [UITableViewCell dequeOrCreateInTable:tableView withId:@"wikiHowCell" andStyle:UITableViewCellStyleDefault];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.title = [[featuredArticles objectAtIndex:indexPath.row] objectForKey:@"title"];
    return cell;
}

   从代码量衡量,仅从此处可能感觉重构前后变化不大,甚至还会略有增多。但如果考虑到公用方法的多次使用产生的“效益”,付出的努力应该是值当的!

本文出自 “林家男孩” 博客,请务必保留此出处http://bj007.blog.51cto.com/1701577/545529

原文地址:https://www.cnblogs.com/simonshi2012/p/2269353.html