java使用策略模式优化代码中的if-else if 判断。

首先,定义策略接口和不同的策略实现类:

java 复制代码
// 策略接口
public interface DiscountStrategy {
    double applyDiscount(double originalPrice);
}

// 学生折扣策略
@Component
public class StudentDiscountStrategy implements DiscountStrategy {
    @Override
    public double applyDiscount(double originalPrice) {
        // 学生折扣为 10%
        return originalPrice * 0.9;
    }
}

// 老年人折扣策略
@Component
public class SeniorCitizenDiscountStrategy implements DiscountStrategy {
    @Override
    public double applyDiscount(double originalPrice) {
        // 老年人折扣为 15%
        return originalPrice * 0.85;
    }
}

// 普通顾客无折扣策略
@Component
public class NoDiscountStrategy implements DiscountStrategy {
    @Override
    public double applyDiscount(double originalPrice) {
        // 普通顾客无折扣
        return originalPrice;
    }
}

在上述代码中,使用了 @Component 注解来将每个折扣策略实现类声明为 Spring 管理的 Bean。

接下来,创建一个服务类,用于根据条件选择合适的折扣策略,并注入对应的策略 Bean:

java 复制代码
@Service
public class DiscountService {
    
    @Autowired
    private StudentDiscountStrategy studentDiscountStrategy;
    
    @Autowired
    private SeniorCitizenDiscountStrategy seniorCitizenDiscountStrategy;
    
    @Autowired
    private NoDiscountStrategy noDiscountStrategy;
    
    public double calculateDiscountedPrice(double originalPrice, String customerType) {
        DiscountStrategy strategy = getDiscountStrategy(customerType);
        return strategy.applyDiscount(originalPrice);
    }
    
    private DiscountStrategy getDiscountStrategy(String customerType) {
        switch (customerType) {
            case "student":
                return studentDiscountStrategy;
            case "senior":
                return seniorCitizenDiscountStrategy;
            default:
                return noDiscountStrategy;
        }
    }
}

DiscountService 中,我们通过 @Autowired 注解将各种折扣策略注入到服务类中,并根据客户类型选择合适的策略。

最后,可以在控制器(Controller)或其他服务中使用 DiscountService

java 复制代码
@RestController
@RequestMapping("/discount")
public class DiscountController {

    @Autowired
    private DiscountService discountService;

    @GetMapping("/calculate")
    public String calculateDiscount(@RequestParam String customerType, @RequestParam double originalPrice) {
        double discountedPrice = discountService.calculateDiscountedPrice(originalPrice, customerType);
        return "Discounted price for " + customerType + ": " + discountedPrice;
    }
}

在这个示例中,DiscountController 中的 calculateDiscount 方法接收客户类型和原始价格作为参数,并调用 DiscountServicecalculateDiscountedPrice 方法来计算折扣后的价格。

通过这种方式,你可以利用 Spring Boot 的依赖注入和管理功能,避免了使用大量的 if-else if 结构,使得代码更加清晰、灵活和可维护。

点点赞点点关注呀,持续分享有用的知识........................

相关推荐
cdg==吃蛋糕23 分钟前
selenium 使用方法
开发语言·python
惊骇世俗王某人1 小时前
8. JVM类装载的执行过程
jvm
爱掉发的小李1 小时前
前端开发中的输出问题
开发语言·前端·javascript
张先shen1 小时前
Spring Boot集成Redis:从配置到实战的完整指南
spring boot·redis·后端
zyx没烦恼1 小时前
五种IO模型
开发语言·c++
cainiao0806052 小时前
Java 大视界:基于 Java 的大数据可视化在智慧城市能源消耗动态监测与优化决策中的应用(2025 实战全景)
java
Q_Q5110082852 小时前
python的婚纱影楼管理系统
开发语言·spring boot·python·django·flask·node.js·php
EutoCool2 小时前
Qt窗口:菜单栏
开发语言·c++·嵌入式硬件·qt·前端框架
长风破浪会有时呀2 小时前
记一次接口优化历程 CountDownLatch
java
云朵大王2 小时前
SQL 视图与事务知识点详解及练习题
java·大数据·数据库