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

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

相关推荐
逸Y 仙X5 分钟前
文章五:Elasticsearch安全通信
java·大数据·安全·elasticsearch·搜索引擎·全文检索·jenkins
jameslogo11 分钟前
类加载机制
jvm
quan263116 分钟前
20260529,日常开发-查老数据全量更新闭坑
java·mysql·主从·主从延迟
大大杰哥18 分钟前
Java 日志框架详解:SLF4J + Logback 从入门到实战
java·开发语言·logback
ylscode25 分钟前
黑客利用 GHOSTYNETWORKS 和 OMEGATECH 托管 JS 恶意软件基础设施
开发语言·安全·php·安全威胁分析
爱吃生蚝的于勒26 分钟前
QT开发第二章——信号和槽
c语言·开发语言·c++·qt
xcLeigh31 分钟前
Python入门:Python3 operator模块全面学习教程
开发语言·python·学习·教程·python3·operator
Dest1ny-安全32 分钟前
2026最新CTF知识库:12大Web漏洞深度文章+1156篇历年大赛WP+50+脚本+Payload速查 +AI/RAG离线在线知识库
java·学习·安全·web安全·servlet
404号扳手32 分钟前
Java 基础知识(六)
java·后端
大叔带刺33 分钟前
使用python创建自己的专属星座签名APP:Name2Constell
开发语言·python·pygame