MST

星途 面试题库

面试题:Kotlin策略模式与高阶函数融合在复杂业务逻辑中的应用

在一个电商系统中,有不同的促销策略,比如满减、折扣、买一送一。这些策略需要结合商品价格、购买数量等多种因素动态计算最终价格。请用Kotlin的策略模式与高阶函数设计并实现这个计算最终价格的模块,详细说明代码的架构以及如何处理不同策略间的切换和参数传递。
47.9万 热度难度
编程语言Kotlin

知识考点

AI 面试

面试题答案

一键面试

1. 定义策略接口

interface PromotionStrategy {
    fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double
}

2. 实现具体策略

满减策略

class FullReductionStrategy(private val threshold: Double, private val reduction: Double) : PromotionStrategy {
    override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
        val totalPrice = originalPrice * quantity
        return if (totalPrice >= threshold) {
            totalPrice - reduction
        } else {
            totalPrice
        }
    }
}

折扣策略

class DiscountStrategy(private val discount: Double) : PromotionStrategy {
    override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
        val totalPrice = originalPrice * quantity
        return totalPrice * discount
    }
}

买一送一策略

class BuyOneGetOneFreeStrategy : PromotionStrategy {
    override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
        val paidQuantity = (quantity + 1) / 2
        return originalPrice * paidQuantity
    }
}

3. 使用策略模式与高阶函数

class ShoppingCart(private val promotionStrategy: PromotionStrategy) {
    fun calculateTotalPrice(originalPrice: Double, quantity: Int): Double {
        return promotionStrategy.calculateFinalPrice(originalPrice, quantity)
    }
}

// 高阶函数形式创建策略
fun createFullReductionStrategy(threshold: Double, reduction: Double): PromotionStrategy {
    return object : PromotionStrategy {
        override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
            val totalPrice = originalPrice * quantity
            return if (totalPrice >= threshold) {
                totalPrice - reduction
            } else {
                totalPrice
            }
        }
    }
}

fun createDiscountStrategy(discount: Double): PromotionStrategy {
    return object : PromotionStrategy {
        override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
            val totalPrice = originalPrice * quantity
            return totalPrice * discount
        }
    }
}

fun createBuyOneGetOneFreeStrategy(): PromotionStrategy {
    return object : PromotionStrategy {
        override fun calculateFinalPrice(originalPrice: Double, quantity: Int): Double {
            val paidQuantity = (quantity + 1) / 2
            return originalPrice * paidQuantity
        }
    }
}

4. 代码架构说明

  • 策略接口PromotionStrategy 定义了所有促销策略需要实现的方法 calculateFinalPrice,接收商品原价和购买数量作为参数,返回最终价格。
  • 具体策略类:每个促销策略都实现了 PromotionStrategy 接口,在 calculateFinalPrice 方法中根据自身逻辑计算最终价格。
  • 购物车类ShoppingCart 类接受一个 PromotionStrategy 实例作为参数,通过调用该实例的 calculateFinalPrice 方法计算总价。这体现了策略模式中通过组合方式使用不同策略。
  • 高阶函数createFullReductionStrategycreateDiscountStrategycreateBuyOneGetOneFreeStrategy 等高阶函数通过返回实现了 PromotionStrategy 接口的对象,方便创建具体策略实例,使得代码更具灵活性。

5. 策略切换和参数传递

  • 策略切换:通过在创建 ShoppingCart 实例时传入不同的 PromotionStrategy 实例来实现策略切换。例如:
val cart1 = ShoppingCart(FullReductionStrategy(100.0, 20.0))
val cart2 = ShoppingCart(DiscountStrategy(0.8))
val cart3 = ShoppingCart(BuyOneGetOneFreeStrategy())
  • 参数传递:对于需要参数的策略(如满减策略的阈值和减免金额、折扣策略的折扣率),通过在创建策略实例时传递相应参数来处理。例如 FullReductionStrategy(100.0, 20.0) 中,100.0 是满减阈值,20.0 是减免金额。