当前位置: 首页 > 后端技术 > Java

SpringBoot实现装饰者模式,真香!

时间:2023-04-02 09:58:09 Java

前言本文结合实战案例介绍我们平时如何使用装饰器模式,以及如何在springboot项目中玩转多层装饰。首先说一下什么是装饰者模式。装饰器模式(DecoratorPattern),也称为包装器模式(WrapperPattern),是指在不改变原有对象的情况下,为对象附加功能,提供比继承更多的支持。灵活的替代方案(扩展原始对象的功能)是结构模式。官方:装饰器模式的核心是功能扩展,使用装饰器模式可以透明动态地扩展类的功能。说白了:有点像组合,就是我不搬原来业务的东西,但是我想给这个业务的东西增加一些额外的职责。非侵入性。可修补的。预热ISurfTheInternetService上网服务接口interfaceFadInternetCafe实现ISurfTheInternetService时尚网咖实现上网服务接口,实现重写,提供上网方法在这个原来的上网业务下,没有代码入侵,我们想给上网业务增加一些额外的职责,比如XXX,XXX等等。于是,我们开始玩起了装饰器设计模式SurfDecoratorimplementsISurfTheInternetService上网装饰器是如何工作的呢?看代码,后面会讲怎么多层装饰。SpringBoot的基础就不介绍了。推荐观看这个免费教程:https://github.com/javastacks/spring-boot-best-practice废话少说。①ISurfTheInternetService.java上网服务接口/***@Author:JCccc*@Date:2022-10-0715:18*@Description:上网*/publicinterfaceISurfTheInternetService{/***冲刺*/voiddoSurfing();}②FadInternetCafe.java时尚网吧业务实现类importcom.example.mydemo.service.ISurfTheInternetService;importorg.springframework.stereotype.Service;/***@Author:JCccc*@Date:2022-10-0715:21*@Description:FadInternetCafe*/@Service("fadInternetCafeService")公共类FadInternetCafe实现ISurfTheInternetService{@OverridepublicvoiddoSurfing(){System.out.println("在FadInternetCafe中冲浪~");}}③RetroInternetBar.java复古网吧业务实现类importcom.example.mydemo.service.ISurfTheInternetService;importorg.springframework.stereotype.Service;/***@Author:JCccc*@Date:2022-10-0715:21*@Description:RetroInternetBar*/@Service("retroInternetBarService")publicclassRetroInternetBarimplementsISurfTheInternetService{@OverridepublicvoiddoSurfing(){System.out.println("复古网吧上网冲浪~");}}先来,写一个controller方法模拟真实的搬砖场景:@Autowired@Qualifier("fadInternetCafeService")ISurfTheInternetServicefadInternetCafeService;@Autowired@Qualifier("retroInternetBarService")ISurfTheInternetServiceretroInternetBarService;@GetMapping("/doTest")publicvoiddoTest(){fadInternetCafeService.doSurfing();retroInternetBarService.doSurfing();}可以看到这样调用的效果de:然后在原来的冲浪业务下,没有代码入侵,我们想给冲浪业务增加一些额外的职责,比如XXX,XXX等xxx企业。④SurfDecorator.java上网装饰器/***@Author:JCccc*@Date:2022-10-0715:29*@Description:*/publicclassSurfDecoratorimplementsISurfTheInternetService{/***内部维护一个上网接口类*/私有ISurfTheInternetServicesurfTheInternetService;/***构造方法将传入类赋值给内部类*@paramsurfTheInternetService*/publicSurfDecorator(ISurfTheInternetServicesurfTheInternetService){this.surfTheInternetService=surfTheInternetService;}/***增强的冲浪方法*/@OverridepublicvoiddoSurfing(){System.out.println("SurfDecorator模拟服务增强器正在玩一些很新的东西,也许还有一些额外的职责....");//增强surfTheInternetService.doSurfing();System.out.println("SurfDecorator模拟业务增强器玩了个很新的东西,可能是一些额外的业务,比如XXXX");}}然后我们通过装饰器调用方法实现Enhancedresponsibility:@GetMapping("/useDecoratorTest")publicvoiduseDecoratorTest(){SurfDecoratorfadInternetCafeDecoratorService=newSurfDecorator(fadInternetC安全服务);fadInternetCafeDecoratorService.doSurfing();SurfDecoratorretroInternetBarDecoratorService=newSurfDecorator(retroInternetBarService);retroInternetBarDecoratorService.doSurfing();}可以看到效果,它安装了:然后,如果我们要装饰多层,也就是针对不同的上网业务实现类,我要一层一层的安装,比如上网时尚网卡生意,网咖老板比较腹黑,不仅要做A增强业务,还想看看来上网的人是不是都是有钱人,所以想查查怎么做卡里有多少钱,好安排一些‘优质服务’⑤另一层装饰器RechargeDecorator.java:ps:继承了基本的上网装饰器,然后增强了自己检测充值金额的业务方法。/***@Author:JCccc*@Date:2022-10-0715:29*@Description:*/publicclassRechargeDecoratorextendsSurfDecorator{publicRechargeDecorator(ISurfTheInternetServicesurfTheInternetService){super(surfTheInternetService);}@OverridepublicvoiddoSurfing(){super.doSurfing();检查充值();}privatevoidcheckRecharge(){System.out.print("RechargeDecorator也增强了,查看这张货卡充了多少,上线");}}然后看我们怎么玩多层装饰:@GetMapping("/moreDecoratorTest")publicvoidmoreDecoratorTest(){//先安装一个SurfDecoratorretroInternetBarDecoratorService=newSurfDecorator(retroInternetBarService);//封装一个RechargeDecoratorrechargeDecorator=newRechargeDecorator(retroInternetBarDecoratorService);rechargeDecorator.doSurfing();}可以看到效果,装了,又装了:来源:blog.csdn.net/qq_35387940/article/details/127464609近期热点文章推荐:1.1000+Java面试题及答案(2022最新版)2.太棒了!Java协程来了。..3.SpringBoot2.x教程,太全面了!4.不要用爆破爆满画面,试试装饰者模式,这才是优雅的方式!!5.《Java开发手册(嵩山版)》最新发布,赶快下载吧!感觉不错,别忘了点赞+转发!