当前位置: 首页 > wzjs >正文

包装纸箱怎么做网站微信绑定网站

包装纸箱怎么做网站,微信绑定网站,WordPress 后台反应好慢,内网做网站外网访问引言​ 在当今的软件开发领域,Spring Boot 以其高效、便捷的特性成为构建 Java 应用程序的热门框架。而设计模式作为软件开发中的宝贵经验总结,能够显著提升代码的可维护性、可扩展性和可复用性。本文将深入探讨几种常用设计模式在 Spring Boot 项目中的…

引言​

在当今的软件开发领域,Spring Boot 以其高效、便捷的特性成为构建 Java 应用程序的热门框架。而设计模式作为软件开发中的宝贵经验总结,能够显著提升代码的可维护性、可扩展性和可复用性。本文将深入探讨几种常用设计模式在 Spring Boot 项目中的实际应用,通过具体案例帮助读者更好地理解和掌握这些设计模式在实际开发中的运用技巧。

一、工厂模式

场景描述​

假设我们正在开发一个电商系统,其中有多种不同类型的订单,如普通订单、促销订单、团购订单等。每种订单在处理时都有一些独特的逻辑,例如促销订单需要计算优惠金额,团购订单需要处理成团逻辑等。我们可以使用工厂模式来根据订单类型创建不同的订单处理对象。​

代码实现

1.定义订单处理接口

public interface OrderProcessor {void processOrder();
}

2.创建具体的订单处理类

  • 普通订单处理类
@Component
public class NormalOrderProcessor implements OrderProcessor {@Overridepublic void processOrder() {System.out.println("Processing normal order...");}
}
  • 促销订单处理类
@Component
public class PromotionOrderProcessor implements OrderProcessor {@Overridepublic void processOrder() {System.out.println("Processing promotion order, calculating discounts...");}
}

3.创建订单工厂类

@Component
public class OrderFactory {@Autowiredprivate Map<String, OrderProcessor> orderProcessorMap;public OrderProcessor getOrderProcessor(String orderType) {return orderProcessorMap.get(orderType);}
}

4.在服务层使用工厂类

@Service
public class OrderService {@Autowiredprivate OrderFactory orderFactory;public void process(String orderType) {OrderProcessor orderProcessor = orderFactory.getOrderProcessor(orderType);if (orderProcessor != null) {orderProcessor.processOrder();} else {System.out.println("No suitable order processor found for type: " + orderType);}}
}

优势分析​

通过工厂模式,我们将对象的创建和使用分离,当需要新增订单类型时,只需创建新的订单处理类并在配置中注册,无需修改大量现有代码,提高了系统的可扩展性。​

二、单例模式​

场景描述​

在 Spring Boot 项目中,经常会有一些资源或对象只需要一个实例,例如数据库连接池、日志记录器等。以日志记录器为例,我们希望整个应用程序中只有一个日志记录器实例,以确保日志记录的一致性和高效性。​

代码实现

1.使用 Spring 的注解实现单例

@Component
public class CustomLogger {private static final org.slf4j.Logger logger = org.slf4j.LoggerFactory.getLogger(CustomLogger.class);public void logMessage(String message) {logger.info(message);}
}

在 Spring 中,默认情况下,通过@Component注解创建的 Bean 就是单例模式。当其他组件需要使用CustomLogger时,直接注入即可。

@Service
public class SomeService {@Autowiredprivate CustomLogger customLogger;public void doSomething() {customLogger.logMessage("Service is doing something...");}
}

优势分析​

单例模式确保了系统中特定资源或对象的唯一性,避免了资源的重复创建和浪费,提高了系统性能和资源利用率,同时也方便了对共享资源的管理和维护。​

三、代理模式​

场景描述​

假设我们有一个业务方法,在调用该方法前后需要添加一些通用的逻辑,比如权限验证、日志记录等。我们可以使用代理模式来实现这些横切关注点,而无需在每个业务方法中重复编写相同的代码。​

代码实现

1.定义业务接口

public interface UserService {void doBusinessLogic();
}

2.创建真实业务类

@Service
public class UserServiceImpl implements UserService {@Overridepublic void doBusinessLogic() {System.out.println("Doing business logic...");}
}

3.创建代理类

@Aspect
@Component
public class UserServiceProxy {@Around("execution(* com.example.demo.service.UserService.doBusinessLogic(..))")public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("Before business logic - checking permissions");Object result = joinPoint.proceed();System.out.println("After business logic - logging");return result;}
}

优势分析​

代理模式将核心业务逻辑与辅助性的横切逻辑分离,使代码更加清晰、可维护。通过代理类,我们可以方便地在不修改原有业务代码的基础上添加新的功能,符合开闭原则。

四、策略模式​

场景描述​

继续以电商系统为例,系统中有多种不同的运费计算方式,如按重量计算、按订单金额计算、固定运费等。为了能够灵活地切换和管理这些运费计算方式,我们可以运用策略模式。​

代码实现

1.定义运费计算策略接口

public interface ShippingFeeStrategy {double calculateShippingFee(double weight, double orderAmount);
}

2.创建具体的运费计算策略类

  • 按重量计算运费策略类
@Component
public class WeightBasedShippingStrategy implements ShippingFeeStrategy {@Overridepublic double calculateShippingFee(double weight, double orderAmount) {// 假设每千克运费为10元return weight * 10;}
}
  • 按订单金额计算运费策略类
@Component
public class OrderAmountBasedShippingStrategy implements ShippingFeeStrategy {@Overridepublic double calculateShippingFee(double weight, double orderAmount) {// 假设订单金额满100免运费,否则收取10元运费return orderAmount >= 100? 0 : 10;}
}
  • 固定运费策略类
@Component
public class FixedShippingStrategy implements ShippingFeeStrategy {@Overridepublic double calculateShippingFee(double weight, double orderAmount) {// 固定运费为5元return 5;}
}

3.创建运费计算服务,使用策略模式

@Service
public class ShippingFeeService {@Autowiredprivate Map<String, ShippingFeeStrategy> shippingFeeStrategyMap;public double calculate(String strategyType, double weight, double orderAmount) {ShippingFeeStrategy strategy = shippingFeeStrategyMap.get(strategyType);if (strategy != null) {return strategy.calculateShippingFee(weight, orderAmount);} else {throw new IllegalArgumentException("No suitable shipping fee strategy found for type: " + strategyType);}}
}

优势分析​

策略模式使得不同的运费计算算法可以独立变化,符合开闭原则。当需要新增运费计算方式时,只需创建新的策略类并实现接口,然后在配置中注册即可,无需修改现有业务代码,大大提高了系统的灵活性和可维护性。同时,不同的策略类可以被复用,提高了代码的复用性。

补充

也可以自定义Match接口实现策略模式,大致思路如下:

1.定义Match接口

public interface Match<T> {boolean match(T var1);
}

2.业务接口继承Match接口

public interface BusinessHandler extends Match<String> {/*** 业务执行方法** @param context 上下文*/void execute(BusinessContext context);
}

3.特定策略类实现业务接口

类一:

@Slf4j
@Component
public class ExactMatchSort implements BusinessHandler {@Setter(onMethod_ = @Autowired)BusinessService businessService;/*** {@inheritDoc}*/@Overridepublic boolean match(String value) {return RuleEnum.EXACT_MATCH.getCode().equals(value);}/*** {@inheritDoc}*/@Overridepublic void execute(BusinessContext context) {// 自定义业务逻辑businessService.exactMatch(context);}
}

类二:

@Slf4j
@Component
public class FuzzyMatchSort implements BusinessHandler {@Setter(onMethod_ = @Autowired)BusinessService businessService;/*** {@inheritDoc}*/@Overridepublic boolean match(String value) {return RuleEnum.FUZZY_MATCH.getCode().equals(value);}/*** {@inheritDoc}*/@Overridepublic void execute(BusinessContext context) {// 自定义业务逻辑businessService.fuzzyMatch(context);}
}

4.定义抽象策略管理器

public abstract class AbstractProcessorManage<T, P extends Match<T>> {private List<P> processorList;public AbstractProcessorManage() {}@Autowiredpublic void setProcessorList(List<P> processorList) {this.processorList = processorList;}public abstract String getCannotMatchMessage();public P getProcessor(T t) {for(P p : this.processorList) {if (p.match(t)) {return p;}}throw new RuntimeException(this.getCannotMatchMessage());}
}

5.定义业务策略管理器

/*** 策略管理器*/
@Component
public class BusinessManger extends AbstractProcessorManage<String, BusinessHandler> {/*** {@inheritDoc}*/@Overridepublic String getCannotMatchMessage() {return "非法的策略";}
}

6.业务流程调用

/*** 业务执行类**/
@Slf4j
@Component
public class StockFilterManager {@Setter(onMethod_ = @Autowired)private BusinessManger businessManger;private void businessExcute(BusinessContext context) {businessManger.getProcessor(context.value).execute(context);}
}

这仅是基础架构,也可以在内层嵌套更多的设计模式,以保证代码的可读性与可维护性。

结论

通过以上在 Spring Boot 项目中的实战案例,我们看到了工厂模式、单例模式、代理模式以及策略模式在不同场景下的应用及其带来的显著优势。在实际项目开发中,合理运用设计模式能够极大地提升代码质量,使项目更加健壮、灵活和易于维护。希望读者通过本文的学习,能够在自己的 Spring Boot 项目中熟练运用这些设计模式,打造出更优秀的软件系统。


文章转载自:

http://SnfdtK7r.xnymt.cn
http://MXeMl330.xnymt.cn
http://oQFnRe4v.xnymt.cn
http://YTjTlVYY.xnymt.cn
http://8kNSMGOs.xnymt.cn
http://QMOH4tET.xnymt.cn
http://4e5nuYyR.xnymt.cn
http://DOfvFIHP.xnymt.cn
http://4Ff2E822.xnymt.cn
http://pIjYmCSw.xnymt.cn
http://vg7vbIqF.xnymt.cn
http://cfzFCrr2.xnymt.cn
http://3jRWOqch.xnymt.cn
http://SxXdd8aE.xnymt.cn
http://tRPgxAGw.xnymt.cn
http://COGDoal4.xnymt.cn
http://PuQX5JeR.xnymt.cn
http://2WpWLKoS.xnymt.cn
http://VcS4bzr2.xnymt.cn
http://wbQUEj8F.xnymt.cn
http://gH02jgoC.xnymt.cn
http://vIxBqvOw.xnymt.cn
http://9mg2jqqQ.xnymt.cn
http://paznHxDv.xnymt.cn
http://qwWLEZrE.xnymt.cn
http://6UyrQdsM.xnymt.cn
http://TGkJPFZQ.xnymt.cn
http://NgaXVMRN.xnymt.cn
http://7mJ9Xqm0.xnymt.cn
http://GESv3LQG.xnymt.cn
http://www.dtcms.com/wzjs/637698.html

相关文章:

  • 东莞制作网站建设的公司wordpress 店铺插件
  • 什么网站可以接单做设计php做网站项目的流程
  • 赣州网站维护关于网站维护的书籍
  • 西安有做网站的吗app优化推广
  • 织梦做的网站怎么上传视频教程网站建设(中企动力)
  • 自己做网站 搜索功能开发公司网站免费建设
  • 做网站还有意义淘气堡网站建设
  • 南宁建站企业平台网
  • 最好的微网站建设公司赣州宏达网站建设
  • 生物科技公司网站模板做私人网站
  • php网站用什么软件海兴县建设工程招标信息网站
  • 南昌网站建设公司咨询WordPress多站点恢复
  • php网站开发视频教程下载珠宝wordpress主题
  • 厦门住房和城乡建设局网站wordpress和公众号对接
  • 网站seo优化效果南宁seo排名首页
  • 织梦网站安装出现404 not found手机网站模板大全
  • 南宁网站建设及推广求会wordpress的人
  • 漯河北京网站建设公司坦克大战网站开发课程设计报告
  • 北京品牌建设网站大冶网站开发
  • 网站建设全包广州硬件开发外包平台
  • 怎么自己做礼品网站微信网站的优势
  • 网站建设用哪个软件网站建设到一半想换一家
  • 东莞企业网站建设价格南昌建设局网站查询塔吊证怎么查
  • wordpress 全站过滤wordpress 仿京东主题
  • 百度云平台建设网站桂林二手房
  • 网站整体运营思路印刷外贸好做吗
  • 在网站上做的h5如何发到微信上珠宝网站建设要以商为本
  • 网站淘宝客 没备案怎么做活动营销方案
  • 用python写一个简单的网站如果使用自己电脑做网站
  • 做网站公司做网站公司有哪些陕西省安康市建设局网站