Cell工厂

背景:最近在进行list页重构,list页的cell会有不同的展现形式,即同一个UITableView上多种cell并存。为了响应这种需求,我们考虑通过cell容器化的方式解决该问题。最理想的解决方法就是通过工厂模式来定制这些cell,当服务端告知我们某一个indexPath的cell的style时,我们就用相应类型的cell去填充。

工厂模式介绍

工厂模式可以分为简单工厂模式, 工厂方法模式, 抽象工厂模式,这三种模式在设计程度上由简单到复杂。下面挨个解释下各自的特点。

简单工厂模式

简单工厂模式是由工厂类直接生产相应的产品(通过类方法),然后在方法中通过switch-case 语句(或者if-else 语句)来决定胜场什么产品。该模式算是工厂模式的一个特例,因为当用户需要新增一种产品时,需要在直接修改工厂方法的switch-case 语句(或if-else 语句),通过添加分支条件来适应更多种情况,由此看出,它违背了开放-封闭原则。

@interface TRIPHotelCellFactory : NSObject

/**

 *  cell工厂方法

 *

 *  @param cellClassName 待新建的cell类名

 *  @param cellModel     cell数据模型

 *  @param indexPath     index索引

 *

 *  @return 目标cell

 */

+ (TRIPHotelBasicCell *)creatCellWithClassName:(NSString *)cellClassName

                                     cellModel:(TRIPHotelCellModel *)cellModel

                                     indexPath:(NSIndexPath *)indexPath;

@end



@implementation TRIPHotelCellFactory



+ (TRIPHotelBasicCell *)creatCellWithClassName:(NSString *)cellClassName

                                     cellModel:(TRIPHotelCellModel *)cellModel

                                     indexPath:(NSIndexPath *)indexPath{

    TRIPHotelBasicCell *cell = nil;



    if ([cellClassName isEqualToString:@"TRIPHotelStandardCell"]) {

        cell = [[TRIPHotelStandardCell alloc] initWithStyle:UITableViewCellStyleDefault

                                            reuseIdentifier:@"TRIPHotelStandardCell"];

    } else if ([cellClassName isEqualToString:@"TRIPHotelForSaleCell"]){

        cell = [[TRIPHotelForSaleCell alloc] initWithStyle:UITableViewCellStyleDefault

                                           reuseIdentifier:@"TRIPHotelForSaleCell"];

    }



    return [cell initData:cellModel indexPath:indexPath];

}

@end

工厂方法模式

工厂方法模式不直接生产产品,而是定义好如何生产产品的接口,然后由其子类决定具体生产何种产品,即工厂方法使一个类的实例化延迟到其子类。基类工厂只负责定义接口即可,具体的实现由其子类完成,当需要新增一种产品类型时,只需要再定义一个子类工厂,由该子工厂类去生产相应的产品。很好的满足了开发-封闭的原则。

@interface TRIPHotelCellFactory : NSObject

/**

 *  cell工厂方法

 *

 *  @param cellModel 待新建的cell类名

 *  @param indexPath cell数据模型

 *

 *  @return 目标cell

 */

+ (TRIPHotelBasicCell *)creatCellWithModel:(TRIPHotelCellModel *)cellModel

                                     indexPath:(NSIndexPath *)indexPath;

@end



@implementation TRIPHotelCellFactory

+ (TRIPHotelBasicCell *)creatCellWithModel:(TRIPHotelCellModel *)cellModel

                                 indexPath:(NSIndexPath *)indexPath{

    return [[TRIPHotelBasicCell alloc] initWithStyle:UITableViewCellStyleDefault

                                     reuseIdentifier:@"TRIPHotelBasicCell"];

}

@end



@interface TRIPHotelForSaleCellFactory : TRIPHotelCellFactory



@end



@implementation TRIPHotelForSaleCellFactory



+ (TRIPHotelBasicCell *)creatCellWithModel:(TRIPHotelCellModel *)cellModel

                                 indexPath:(NSIndexPath *)indexPath{

    TRIPHotelForSaleCell *cell = [[TRIPHotelForSaleCell alloc] initWithStyle:UITableViewCellStyleDefault

                                                             reuseIdentifier:@"TRIPHotelForSaleCell"];

    return [cell initData:cellModel indexPath:indexPath];

}



@end

实际上基类工厂方法中返回什么都无所谓,最终我们是其子类去生产相应的产品。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值