分享免费的编程资源和教程

网站首页 > 技术教程 正文

Spring Boot 结合设计模式实战:以工厂模式为例

goqiw 2025-01-12 13:54:01 技术教程 30 ℃ 0 评论

Spring Boot 结合设计模式实战:以工厂模式为例

在现代软件开发中,Spring Boot 已经成为了构建后端应用的热门选择,而合理运用设计模式能让我们的代码更加健壮、易于维护和扩展。今天,就以工厂模式为例,为大家详细讲解 Spring Boot 是如何与之完美结合,解决实际开发中的问题。

一、工厂模式简介

工厂模式属于创建型设计模式,它提供了一种创建对象的方式,将对象的创建和使用分离。通过一个工厂类来负责创建对象,而不是让客户端代码直接通过 new 关键字实例化对象。这样做的好处有很多,例如当对象的创建过程复杂,涉及到诸多初始化操作、依赖注入时,工厂模式能把这些复杂性封装起来,让代码的依赖关系更加清晰。

二、实战场景设定

假设我们正在开发一个电商系统,其中有不同类型的订单,如普通订单、团购订单、促销订单等。每种订单都有自己独特的处理逻辑,但它们又有一些共性的方法,比如计算总价、获取订单详情等。在 Spring Boot 项目中,我们希望能够灵活地创建这些不同类型的订单对象,这时候工厂模式就能派上用场。

三、代码实现

1. 定义订单抽象类

首先,创建一个抽象的订单类 AbstractOrder,它包含了一些公共的方法签名:

public abstract class AbstractOrder {

protected String orderId;

protected List<Item> items;

public abstract double calculateTotalPrice();

public String getOrderId() {

return orderId;

}

public void setOrderId(String orderId) {

this.orderId = orderId;

}

public List<Item> getItems() {

return items;

}

public void setItems(List<Item> items) {

this.items = items;

}

}

这里的 Item 可以是一个简单的商品实体类,包含商品名称、价格等属性,为了简洁,代码省略。

2. 具体订单类实现

接着,实现具体的订单类,比如普通订单 NormalOrder:

@Component

public class NormalOrder extends AbstractOrder {

@Override

public double calculateTotalPrice() {

double total = 0;

for (Item item : getItems()) {

total += item.getPrice();

}

return total;

}

}

团购订单 GroupOrder:

@Component

public class GroupOrder extends AbstractOrder {

@Override

public double calculateTotalPrice() {

double total = 0;

for (Item item : getItems()) {

total += item.getPrice();

}

// 假设团购有 10% 的折扣

return total * 0.9;

}

}

促销订单 PromotionOrder:

@Component

public class PromotionOrder extends AbstractOrder {

@Override

public double calculateTotalPrice() {

double total = 0;

for (Item item : getItems()) {

total += item.getPrice();

}

// 促销活动满 100 减 20

if (total >= 100) {

total -= 20;

}

return total;

}

}

注意,这里我们都使用了 @Component 注解将这些类交给 Spring 容器管理,方便后续依赖注入。

3. 创建订单工厂类

然后,创建订单工厂类 OrderFactory:

@Component

public class OrderFactory {

@Autowired

private ApplicationContext applicationContext;

public AbstractOrder createOrder(String orderType) {

if ("normal".equals(orderType)) {

return applicationContext.getBean(NormalOrder.class);

} else if ("group".equals(orderType)) {

return applicationContext.getBean(GroupOrder.class);

} else if ("promotion".equals(orderType)) {

return applicationContext.getBean(PromotionOrder.class);

}

throw new IllegalArgumentException("Invalid order type: " + orderType);

}

}

在工厂类中,我们通过 @Autowired 注入了 ApplicationContext,利用它根据订单类型从 Spring 容器中获取对应的订单实例。

四、在 Spring Boot 中使用工厂

在 Spring Boot 的控制器或者服务层中,我们可以轻松地使用这个工厂来创建订单:

@RestController

public class OrderController {

@Autowired

private OrderFactory orderFactory;

@GetMapping("/createOrder/{orderType}")

public AbstractOrder createOrder(@PathVariable String orderType) {

return orderFactory.createOrder(orderType);

}

}

这样,当客户端访问 /createOrder/normal 这样的接口时,就能得到一个普通订单对象,访问 /createOrder/group 就能得到团购订单对象,依此类推。

五、总结

通过这个简单的实战案例,我们可以看到 Spring Boot 和工厂模式结合带来的诸多优势。一方面,工厂模式让订单对象的创建逻辑更加清晰,易于维护和扩展,当后续有新的订单类型加入时,只需要在工厂类中添加相应的创建逻辑即可。另一方面,借助 Spring Boot 的依赖注入和组件管理功能,使得代码的耦合度更低,各个组件能够更加灵活地协作。在实际开发中,大家可以根据不同的业务场景,灵活运用各种设计模式,让 Spring Boot 项目的架构更加优秀。希望这篇博客能帮助大家开启 Spring Boot 与设计模式结合的探索之旅。

本文暂时没有评论,来添加一个吧(●'◡'●)

欢迎 发表评论:

最近发表
标签列表