工厂模式在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;

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

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

猜你喜欢

转载自blog.csdn.net/LIUXIAOXIAOBO/article/details/121213452