徒手撸设计模式-装饰器模式

打印 上一主题 下一主题

主题 999|帖子 999|积分 2997

概念

装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。
这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。
我们通过下面的实例来演示装饰器模式的用法。其中,我们将把一个形状装饰上不同的颜色,同时又不改变形状类。
参考链接: https://www.runoob.com/design-pattern/decorator-pattern.html
代码案例

新增车子接口
  1. /**
  2. * 生产车子接口
  3. */
  4. public interface Cars {
  5.     String product();
  6. }
复制代码
 
三个实现类

自行车
  1. /**
  2. * 生产自行车
  3. */
  4. @Slf4j
  5. public class   Bicycle implements Cars {
  6.     @Override
  7.     public String product() {
  8.         log.info("生产自行车");
  9.         return "自行车";
  10.     }
  11. }
复制代码
 
汽车
  1. /**
  2. * 生产汽车
  3. */
  4. @Slf4j
  5. public class Car implements Cars {
  6.     @Override
  7.     public String product() {
  8.         log.info("生产汽车");
  9.         return "汽车";
  10.     }
  11. }
复制代码
 
车子装饰器
  1. /**
  2. * 车子装饰类
  3. */
  4. @Slf4j
  5. public abstract class CarsDecorator implements Cars {
  6.     protected Cars decoratorCars;
  7.     public CarsDecorator(Cars decoratorCars) {
  8.         this.decoratorCars = decoratorCars;
  9.     }
  10.     @Override
  11.     public String product() {
  12.         return decoratorCars.product();
  13.     }
  14. }
复制代码
 
颜色装饰器
  1. /**
  2. * 土豪金色车子装饰器
  3. */
  4. public class GoldenCarsDecorator extends CarsDecorator {
  5.     public GoldenCarsDecorator(Cars decoratorCars) {
  6.         super(decoratorCars);
  7.     }
  8.     @Override
  9.     public String product() {
  10.         String product = decoratorCars.product();
  11.         return setColour(decoratorCars)+"----"+product;
  12.     }
  13.     public String setColour(Cars decoratorCars){
  14.         decoratorCars.product();
  15.         return "土豪金色";
  16.     }
  17. }
复制代码
 
测试主类
  1. /**
  2. * 设计模式控制器
  3. */
  4. @RestController
  5. @RequestMapping("/designPattern")
  6. @Slf4j
  7. public class DesignController {
  8.     @GetMapping("/decorator")
  9.     public ResponseModel decorator() {
  10.         com.koukay.student.design.decorator.Cars bicycle= new com.koukay.student.design.decorator.impl.Bicycle();
  11.         CarsDecorator cdCar = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Car());
  12.         CarsDecorator cdBicycle = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Bicycle());
  13.         com.koukay.student.design.decorator.Cars cdCarCars = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Car());
  14.         com.koukay.student.design.decorator.Cars cdBicycleCars = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Bicycle());
  15.         String bicyclePS = bicycle.product();
  16.         String CarD = cdCar.product();
  17.         String bicycleD = cdBicycle.product();
  18.         String carP = cdCarCars.product();
  19.         String bicycleP = cdBicycleCars.product();
  20.         LinkedHashMap map= new LinkedHashMap();
  21.         map.put("bicyclePS",bicyclePS);
  22.         map.put("CarD",CarD);
  23.         map.put("bicycleD",bicycleD);
  24.         map.put("carP",carP);
  25.         map.put("bicycleP",bicycleP);
  26.         return new ResponseModel("装饰器模式完成", 200, map);
  27.     }
  28. }
复制代码
 
测试案例


 
 
  1. 2022-06-25 01:05:47.315 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
  2. 2022-06-25 01:05:47.316 INFO  生产汽车 【http-nio-8081-exec-7】【Car:13】
  3. 2022-06-25 01:05:47.317 INFO  生产汽车 【http-nio-8081-exec-7】【Car:13】
  4. 2022-06-25 01:05:47.326 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
  5. 2022-06-25 01:05:47.328 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
  6. 2022-06-25 01:05:47.328 INFO  生产汽车 【http-nio-8081-exec-7】【Car:13】
  7. 2022-06-25 01:05:47.328 INFO  生产汽车 【http-nio-8081-exec-7】【Car:13】
  8. 2022-06-25 01:05:47.329 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
  9. 2022-06-25 01:05:47.329 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
复制代码
 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

美食家大橙子

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表