工厂模式在iOS开发中的实际应用

使用场景

如果一组相关对象在运行时按不同的标准创建的不一样,此时使用者就需要知道全部的细节才能创建他们,如果情况增加,代码就会变得难以维护

好处

1 工厂方法在变更返回哪一种对象这一点上更灵活
2工厂方法模式让使用者要求工厂方法创建的对象拥有一组共同的行为,所以使用者结构中引入新的具体类并不需要修改使用者代码,因为返回的任何具体对象都跟使用者在用的以前的接口相同

总结起来就是便于扩展和维护

使用步骤

抽象类使用工厂方法生成具体子类,抽象类定义所有相关子类的共有的共同行为

如代码
父类

@interface VVProductDetailBottomBubbleContentView : UIView

/**
 返回需要展示的view类型

 @param product 商品模型
 @return 类
 */
+ (Class)contentClassWithProduct:(VVProductDetailModel *)product;
/**
 刷新倒计时

 @param timeStr 时间字符串
 */
- (void)loadRemindTime:(NSString *)timeStr;
/**
 展示商品信息

 @param product 商品模型
 */
- (void)loadWithProduct:(VVProductDetailModel *)product;
/**
 已提醒过

 @param hasSetRemind 是否提醒过
 */
- (void)updateWithHasSet:(BOOL)hasSetRemind;

@end


@implementation VVProductDetailBottomBubbleContentView

+ (Class)contentClassWithProduct:(VVProductDetailModel *)product
{
    Class contentClass;
    switch (product.detailType) {
        case VVProductDetailViewTypeSpike:
        {
            contentClass = [VVProductDetailBottomNextView class];
            break;
        }
        case VVProductDetailViewTypeFlashOnsale:
        case VVProductDetailViewTypeBlackFridayOnsale:
        {
            contentClass = [VVProductDetailBottomCountDownView class];
            break;
        }
        case VVProductDetailViewTypeUpComing:
        case VVProductDetailViewTypeBlackFridayUpcoming:
        case VVProductDetailViewTypeBlackFridaySpikeUpcoming:
        {
            contentClass = [VVProductDetailUpcomingBottomBubbleView class];
            break;
        }
        case VVProductDetailViewTypeNormal:
        case VVProductDetailViewTypeDailySelection:
        case VVProductDetailViewTyperRankingList:
        case VVProductDetailViewTypePromptActivity:
        default:
        {
            contentClass = [VVProductDetailBottomBubbleContentView class];
            break;
        }
    }
    return contentClass;
}

- (void)loadRemindTime:(NSString *)timeStr
{
    ///定义接口
}

- (void)loadWithProduct:(VVProductDetailModel *)product
{
     ///定义接口
}

- (void)updateWithHasSet:(BOOL)hasSetRemind
{
     ///定义接口
}

各个具体工厂重载抽象父类工厂中定义的方法,各个子类拥有一组共同的行为,但实际实现不同

各个子类

/**
 商详页底部倒计时view
 */
@interface VVProductDetailBottomCountDownView : VVProductDetailBottomBubbleContentView

- (void)updateBackgroundImg:(NSString *)imageName;

@end

@implementation VVProductDetailBottomCountDownView

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
        [self setContraints];
    }
    return self;
}

- (void)setupUI
{
    [self addSubview:self.countDownBackgroundButton];
    [self.countDownBackgroundButton addSubview:self.endInLabel];
    [self.countDownBackgroundButton addSubview:self.countDownView];
}

#pragma mark - reload
- (void)loadRemindTime:(NSString *)time
{
	///重写父类的方法
}

- (void)loadWithProduct:(VVProductDetailModel *)product
{
   ///重写父类的方法
}

- (void)updateBackgroundImg:(NSString *)imageName
{
///重写父类的方法
}

其他子类同上

持有者和从工厂得到的具体类之间没有耦合

    Class contentClass = [VVProductDetailBottomBubbleContentView contentClassWithProduct:self.productDetailVM.productModel];
    NSObject *bottomBubbleView = [[contentClass alloc] init];
    if (![bottomBubbleView isKindOfClass:[VVProductDetailBottomBubbleContentView class]]) {
        return;
    }
    self.bottomBubbleView = (VVProductDetailBottomBubbleContentView *)bottomBubbleView;

一个实际使用工厂模式的场景

如下图的情况就适合使用工厂模式,解耦,
请添加图片描述
请添加图片描述
请添加图片描述
请添加图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值