工厂模式

2018-05-04  本文已影响17人  云天涯丶

看到一篇比较好的介绍工厂模式的文章,摘过来作为参考

文章中介绍的三种模式——1、简单工厂模式 2、工厂方法模式 3、抽象工厂模式——本文是简单工厂模式运用——多种cell在tableView或collectionView上显示。

注:本文是在hanzhanbing/Factory基础上做了一些修改。

Demo介绍

DC2CF489-874B-45AA-B2C3-BEBE6CB38DAE.png

1、cell基类和cell子类:
cell基类:不做具体处理,只提供相关接口

#import <UIKit/UIKit.h>
#import "CellModel.h"

@interface BaseTableViewCell : UITableViewCell

// 布局
- (void)initSubviews;

// 赋值
- (void)assignmentOperation:(CellModel *)cellModel;

// 标识符
+ (NSString *)getReuseIdentifier;


@end

@implementation BaseTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        // 布局
        [self initSubviews];
    }
    return self;
}

// 布局
- (void)initSubviews{
    // 空实现
}

// 赋值
- (void)assignmentOperation:(CellModel *)cellModel{
    // 空实现
}

// 标识符
+ (NSString *)getReuseIdentifier{
    // 空实现
    return nil;
}

@end

cell子类:主要的劳动者

@implementation WordTableViewCell

// 布局
- (void)initSubviews{
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:18];
    _contentLabel.numberOfLines = 0;
    _contentLabel.textColor = [UIColor blackColor];
    [self.contentView addSubview:_contentLabel];
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(30);
        make.left.mas_equalTo(30);
        make.right.mas_equalTo(-30);
    }];
    
    _noteLabel = [[UILabel alloc] init];
    _noteLabel.font = [UIFont systemFontOfSize:14];
    _noteLabel.textColor = kHexColor(0x9B9B9B);
    [self.contentView addSubview:_noteLabel];
    [_noteLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentLabel.mas_bottom).offset(15);
        make.left.equalTo(self.contentLabel.mas_left);
        make.right.equalTo(self.contentLabel.mas_right);
        make.bottom.mas_lessThanOrEqualTo(-30);
    }];
}

// 赋值
- (void)assignmentOperation:(CellModel *)cellModel{
    _contentLabel.text = cellModel.content;
    _noteLabel.text = cellModel.note;
}

// 标识符
+ (NSString *)getReuseIdentifier{
    return [NSString stringWithFormat:@"%@",self];
}

@end

2、模型和本地数据:
数据及模型

3、工厂:
主要的入口,对传入的数据进行分类,交给相关cell子类处理

@interface Factory : NSObject

+ (BaseTableViewCell *)factoryClassifierWithType:(NSString *)cellType cellStyle:(UITableViewCellStyle)cellStyle reuseIdentifier:(NSString *)reuseIdentifier;

+ (NSString *)getCellIdentifierWithType:(NSString *)cellType;

@end

@implementation Factory

+ (BaseTableViewCell *)factoryClassifierWithType:(NSString *)cellType cellStyle:(UITableViewCellStyle)cellStyle reuseIdentifier:(NSString *)reuseIdentifier{

    if (!cellType || [cellType isEqualToString:@""]) {
        return nil;
    }

    Class class = NSClassFromString(cellType);
    BaseTableViewCell *cell = [[class alloc] initWithStyle:cellStyle reuseIdentifier:reuseIdentifier];
    if (cell) {
        return cell;
    }

    return nil;
}

+ (NSString *)getCellIdentifierWithType:(NSString *)cellType{

    if (!cellType || [cellType isEqualToString:@""]) {
        return nil;
    }

    Class class = NSClassFromString(cellType);

    return [class getReuseIdentifier];
}

@end

附上demo地址:https://github.com/skylineU/FactoryPattern

上一篇 下一篇

猜你喜欢

热点阅读