MST

星途 面试题库

面试题:Objective-C策略模式在复杂业务场景中的优化

在一个电商APP中,有多种促销策略,如满减、折扣、赠品等。每个促销策略可能会涉及到复杂的计算逻辑和与后端数据的交互。请用Objective-C设计一个基于策略模式的架构来管理这些促销策略,并说明如何优化代码以提高可维护性和扩展性,尤其是在新的促销策略不断加入的情况下。
19.7万 热度难度
编程语言Objective-C

知识考点

AI 面试

面试题答案

一键面试

1. 策略模式设计

  1. 定义抽象策略类
#import <Foundation/Foundation.h>

// 抽象促销策略类
@interface PromotionStrategy : NSObject

// 计算促销结果
- (CGFloat)calculatePromotionWithOriginalPrice:(CGFloat)originalPrice;

@end
  1. 具体策略类实现
    • 满减策略
#import "PromotionStrategy.h"

@interface FullReductionStrategy : PromotionStrategy

@property (nonatomic, assign) CGFloat fullAmount;
@property (nonatomic, assign) CGFloat reductionAmount;

- (instancetype)initWithFullAmount:(CGFloat)fullAmount reductionAmount:(CGFloat)reductionAmount;

@end

@implementation FullReductionStrategy

- (instancetype)initWithFullAmount:(CGFloat)fullAmount reductionAmount:(CGFloat)reductionAmount {
    self = [super init];
    if (self) {
        _fullAmount = fullAmount;
        _reductionAmount = reductionAmount;
    }
    return self;
}

- (CGFloat)calculatePromotionWithOriginalPrice:(CGFloat)originalPrice {
    if (originalPrice >= self.fullAmount) {
        return originalPrice - self.reductionAmount;
    }
    return originalPrice;
}

@end
- **折扣策略**
#import "PromotionStrategy.h"

@interface DiscountStrategy : PromotionStrategy

@property (nonatomic, assign) CGFloat discount;

- (instancetype)initWithDiscount:(CGFloat)discount;

@end

@implementation DiscountStrategy

- (instancetype)initWithDiscount:(CGFloat)discount {
    self = [super init];
    if (self) {
        _discount = discount;
    }
    return self;
}

- (CGFloat)calculatePromotionWithOriginalPrice:(CGFloat)originalPrice {
    return originalPrice * self.discount;
}

@end
- **赠品策略(这里简单示例,实际可能涉及更复杂逻辑和后端交互)**
#import "PromotionStrategy.h"

@interface GiftStrategy : PromotionStrategy

// 这里可以添加赠品相关属性,如赠品ID等
@property (nonatomic, strong) NSString *giftID;

- (instancetype)initWithGiftID:(NSString *)giftID;

@end

@implementation GiftStrategy

- (instancetype)initWithGiftID:(NSString *)giftID {
    self = [super init];
    if (self) {
        _giftID = giftID;
    }
    return self;
}

- (CGFloat)calculatePromotionWithOriginalPrice:(CGFloat)originalPrice {
    // 赠品策略可能不直接影响价格,这里返回原价
    return originalPrice;
}

@end
  1. 上下文类
#import <Foundation/Foundation.h>
#import "PromotionStrategy.h"

// 促销策略上下文类
@interface PromotionContext : NSObject

@property (nonatomic, strong) PromotionStrategy *strategy;

- (instancetype)initWithStrategy:(PromotionStrategy *)strategy;

- (CGFloat)executePromotionWithOriginalPrice:(CGFloat)originalPrice;

@end

@implementation PromotionContext

- (instancetype)initWithStrategy:(PromotionStrategy *)strategy {
    self = [super init];
    if (self) {
        _strategy = strategy;
    }
    return self;
}

- (CGFloat)executePromotionWithOriginalPrice:(CGFloat)originalPrice {
    return [self.strategy calculatePromotionWithOriginalPrice:originalPrice];
}

@end
  1. 使用示例
#import <UIKit/UIKit.h>
#import "PromotionContext.h"
#import "FullReductionStrategy.h"
#import "DiscountStrategy.h"
#import "GiftStrategy.h"

@interface ViewController : UIViewController

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 满减策略
    FullReductionStrategy *fullReduction = [[FullReductionStrategy alloc] initWithFullAmount:200 reductionAmount:50];
    PromotionContext *fullReductionContext = [[PromotionContext alloc] initWithStrategy:fullReduction];
    CGFloat fullReductionResult = [fullReductionContext executePromotionWithOriginalPrice:300];
    NSLog(@"满减结果: %f", fullReductionResult);
    
    // 折扣策略
    DiscountStrategy *discount = [[DiscountStrategy alloc] initWithDiscount:0.8];
    PromotionContext *discountContext = [[PromotionContext alloc] initWithStrategy:discount];
    CGFloat discountResult = [discountContext executePromotionWithOriginalPrice:300];
    NSLog(@"折扣结果: %f", discountResult);
    
    // 赠品策略
    GiftStrategy *gift = [[GiftStrategy alloc] initWithGiftID:@"gift001"];
    PromotionContext *giftContext = [[PromotionContext alloc] initWithStrategy:gift];
    CGFloat giftResult = [giftContext executePromotionWithOriginalPrice:300];
    NSLog(@"赠品结果: %f", giftResult);
}

@end

2. 代码优化以提高可维护性和扩展性

  1. 单一职责原则:每个具体策略类只负责一种促销策略的计算逻辑,如满减策略类只处理满减相关计算,这样代码职责清晰,易于维护。
  2. 依赖注入:通过PromotionContext类的initWithStrategy:方法,将具体的促销策略对象注入到上下文中,使得上下文与具体策略解耦,便于替换不同的策略。
  3. 模块化管理:将不同的促销策略类放在不同的文件中,便于管理和查找。同时,对于复杂的后端交互逻辑,可以封装在具体策略类的私有方法中,避免污染外部接口。
  4. 扩展性优化:当有新的促销策略加入时,只需要创建一个新的具体策略类继承自PromotionStrategy,实现calculatePromotionWithOriginalPrice:方法即可。然后在需要使用该策略的地方,通过PromotionContext注入新的策略对象,不需要修改原有策略类的代码,符合开闭原则,提高了代码的扩展性。