中等
技术面试0 次浏览在拼多多的促销活动中,商品可能会有多种优惠策略,如满减、折扣等。设计一个促销系统,要求能够根据商品信息和用户选择的优惠策略,计算出最终的商品价格。请写出该系统的类设计和主要方法。
拼多多算法工程师
系统设计促销活动商品价格计算
答题要点
推荐使用面向对象的设计思路,采用分层设计的答题框架。关键要点如下:1. 定义商品类:包含商品的基本信息,如原价、名称等。2. 定义优惠策略接口:包含计算优惠后价格的抽象方法。3. 实现不同的优惠策略类:如满减类、折扣类,实现优惠策略接口的方法。4. 定义促销系统类:接收商品和优惠策略,调用相应的策略计算最终价格。示例代码:python class Product: def __init__(self, price): self.price = price class PromotionStrategy: def calculate_price(self, product): pass class DiscountStrategy(PromotionStrategy): def __init__(self, discount): self.discount = discount def calculate_price(self, product): return product.price * self.discount class PromotionSystem: def __init__(self, product, strategy): self.product = product self.strategy = strategy def get_final_price(self): return self.strategy.calculate_price(self.product)