关于java:Spring-Boot-实现装饰器模式真香

3次阅读

共计 4024 个字符,预计需要花费 11 分钟才能阅读完成。

前言

本文配合实战案例介绍咱们平时 在 springboot 我的项目外面 怎么去用 装璜器模式、多层装璜怎么玩。

首先先说下装璜器模式是什么

装璜器模式(Decorator Pattern)也称为包装模式 (Wrapper Pattern) 是指在不扭转原有对象的根底之上,将性能附加到对象上,提供了比继承更有弹性的代替计划 (扩大原有对象的性能),属于结构型模式。

官网:

装璜器模式的外围是性能扩大,应用装璜器模式能够通明且动静地扩大类的性能。

大白话一点:

有点像是 组合,就是 我不动原先的业务货色,然而 又想给这个业务货色 加点额定的职责货色。

  • 非入侵的。
  • 可拼凑的。

实战开搞

实例简述预热

ISurfTheInternetService 网上冲浪冲浪业务 接口 interface

  • FadInternetCafe implements ISurfTheInternetService 时尚网咖 实现冲浪业务接口,实现重写提供 网上冲浪办法
  • RetroInternetBar implements ISurfTheInternetService 复旧网吧 实现冲浪业务接口,实现重写提供 网上冲浪办法

而后在这个原有的冲浪业务下,不做代码入侵,咱们想给网上冲浪冲浪业务加一点额定的职责,比方 XXX,XXX 啥的 xxx 业务。

于是乎,咱们开始玩装璜器设计模式

  • SurfDecorator implements ISurfTheInternetService 网上冲浪装璜器

怎么玩的?看代码,前面还会说怎么多层装璜。

Spring Boot 根底就不介绍了,举荐看这个收费教程:

https://github.com/javastacks/spring-boot-best-practice

事不宜迟。

① ISurfTheInternetService.java 网上冲浪冲浪业务 接口
/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:18
 * @Description: 网上冲浪
 */
public interface ISurfTheInternetService {

    /**
     * 冲起来
     */
    void doSurfing();}
② FadInternetCafe.java 时尚网咖业务实现类
import com.example.mydemo.service.ISurfTheInternetService;
import org.springframework.stereotype.Service;

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:21
 * @Description: 时尚 网咖
 */
@Service("fadInternetCafeService")
public class FadInternetCafe implements ISurfTheInternetService {

    @Override
    public void doSurfing() {System.out.println("在时尚 网咖 , 网上冲浪咯~");

    }
}
③ RetroInternetBar.java 复旧网吧业务实现类
import com.example.mydemo.service.ISurfTheInternetService;
import org.springframework.stereotype.Service;

/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:21
 * @Description: 复旧 网吧
 */
@Service("retroInternetBarService")
public class RetroInternetBar implements ISurfTheInternetService {

    @Override
    public void doSurfing() {System.out.println("在复旧 网吧 , 网上冲浪咯~");
    }
}

先到这,写个 controller 办法,模仿平时搬砖实在场景:

@Autowired
@Qualifier("fadInternetCafeService")
ISurfTheInternetService fadInternetCafeService;

@Autowired
@Qualifier("retroInternetBarService")
ISurfTheInternetService retroInternetBarService;

@GetMapping("/doTest")
public void doTest() {fadInternetCafeService.doSurfing();
    retroInternetBarService.doSurfing();}

能够看到调用成果是这样的:

而后。

而后在这个原有的冲浪业务下,不做代码入侵,咱们想给网上冲浪冲浪业务加一点额定的职责,比方 XXX,XXX 啥的 xxx 业务。

④ SurfDecorator.java 网上冲浪装璜器
/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:29
 * @Description:
 */
public class SurfDecorator implements  ISurfTheInternetService {

    /**
     * 外部保护一个冲浪接口类
     */
    private ISurfTheInternetService surfTheInternetService;

    /**
     * 构造方法 把传入的 类 赋值给外部类
     * @param surfTheInternetService
     */
    public SurfDecorator(ISurfTheInternetService surfTheInternetService) {this.surfTheInternetService = surfTheInternetService;}

    /**
     * 加强的网上冲浪办法
     */
    @Override
    public void doSurfing() {System.out.println("SurfDecorator 模仿业务 增强器在玩一点很新的货色, 可能是一些额定的职责业务....");
        // 加强
        surfTheInternetService.doSurfing();
        System.out.println("SurfDecorator 模仿业务 增强器在玩一点很新的货色, 可能是一些额定的职责业务,比如说是 XXXX");
    }

}

而后咱们通过装璜器去 调用办法,实现加强职责:

@GetMapping("/useDecoratorTest")
public void useDecoratorTest() {SurfDecorator fadInternetCafeDecoratorService = new SurfDecorator(fadInternetCafeService);
    fadInternetCafeDecoratorService.doSurfing();

    SurfDecorator retroInternetBarDecoratorService = new SurfDecorator(retroInternetBarService);
    retroInternetBarDecoratorService.doSurfing();}

能够看到成果,它装起来了:

而后,如果咱们想多层装璜,也就是,针对不同也网上冲浪业务实现类,想装一层又一层,

比方 时尚网卡的网上冲浪业务,网咖老板比拟腹黑,不仅仅须要做 A 加强业务,

还想看看每个来上网的人到底是不是有钱人,所以想检测一下卡外面的钱有多少,好安顿一些‘优质服务’。

⑤ 又一层装璜器 RechargeDecorator.java:
ps:它继承了根本的网上冲浪装璜器,而后加强了本人的检测充值金额业务办法。
/**
 * @Author: JCccc
 * @Date: 2022-10-07 15:29
 * @Description:
 */
public class RechargeDecorator extends SurfDecorator{public RechargeDecorator(ISurfTheInternetService surfTheInternetService) {super(surfTheInternetService);
    }

    @Override
    public void doSurfing() {super.doSurfing();
        checkRecharge();}
    private void checkRecharge(){System.out.print("RechargeDecorator 也在加强, 看看这个货卡外面充了有多少, 就来上网");
    }

}

而后看看 咱们怎么玩 多层装璜:

@GetMapping("/moreDecoratorTest")
public void moreDecoratorTest() {

    // 先装一哈
    SurfDecorator retroInternetBarDecoratorService = new SurfDecorator(retroInternetBarService);
    // 再包装一哈
    RechargeDecorator rechargeDecorator = new RechargeDecorator(retroInternetBarDecoratorService);
    rechargeDecorator.doSurfing();}

能够看到成果,它装起来了,又装起来了:

起源:blog.csdn.net/qq_35387940/article/details/127464609

近期热文举荐:

1.1,000+ 道 Java 面试题及答案整顿 (2022 最新版)

2. 劲爆!Java 协程要来了。。。

3.Spring Boot 2.x 教程,太全了!

4. 别再写满屏的爆爆爆炸类了,试试装璜器模式,这才是优雅的形式!!

5.《Java 开发手册(嵩山版)》最新公布,速速下载!

感觉不错,别忘了顺手点赞 + 转发哦!

正文完
 0