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

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

相关推荐
昵称为空C3 小时前
SpringBoot3 http接口调用新方式RestClient + @HttpExchange像使用Feign一样调用
spring boot·后端
架构师沉默3 小时前
设计多租户 SaaS 系统,如何做到数据隔离 & 资源配额?
java·后端·架构
Java中文社群4 小时前
重要:Java25正式发布(长期支持版)!
java·后端·面试
每天进步一点_JL5 小时前
JVM 类加载:双亲委派机制
java·后端
用户298698530146 小时前
Java HTML 转 Word 完整指南
java·后端
渣哥6 小时前
原来公平锁和非公平锁差别这么大
java
渣哥6 小时前
99% 的人没搞懂:Semaphore 到底是干啥的?
java
J2K6 小时前
JDK都25了,你还没用过ZGC?那真得补补课了
java·jvm·后端
kfyty7256 小时前
不依赖第三方,不销毁重建,loveqq 框架如何原生实现动态线程池?
java·架构
isysc18 小时前
面了一个校招生,竟然说我是老古董
java·后端·面试