您的位置:首页 > 其它

Cell工厂

2016-01-22 21:43 351 查看
背景:最近在进行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


实际上基类工厂方法中返回什么都无所谓,最终我们是其子类去生产相应的产品。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: