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 结构,使得代码更加清晰、灵活和可维护。

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

相关推荐
fouryears_234172 小时前
Flutter InheritedWidget 详解:从生命周期到数据流动的完整解析
开发语言·flutter·客户端·dart
我好喜欢你~3 小时前
C#---StopWatch类
开发语言·c#
桦说编程4 小时前
Java 中如何创建不可变类型
java·后端·函数式编程
lifallen4 小时前
Java Stream sort算子实现:SortedOps
java·开发语言
IT毕设实战小研4 小时前
基于Spring Boot 4s店车辆管理系统 租车管理系统 停车位管理系统 智慧车辆管理系统
java·开发语言·spring boot·后端·spring·毕业设计·课程设计
CCCC13101635 小时前
嵌入式学习(day 28)线程
jvm·学习
没有bug.的程序员5 小时前
JVM 总览与运行原理:深入Java虚拟机的核心引擎
java·jvm·python·虚拟机
一只爱撸猫的程序猿5 小时前
使用Spring AI配合MCP(Model Context Protocol)构建一个"智能代码审查助手"
spring boot·aigc·ai编程
甄超锋5 小时前
Java ArrayList的介绍及用法
java·windows·spring boot·python·spring·spring cloud·tomcat
cui__OaO5 小时前
Linux软件编程--线程
linux·开发语言·线程·互斥锁·死锁·信号量·嵌入式学习