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


    概念

    装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构。这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装。

    这种模式创建了一个装饰类,用来包装原有的类,并在保持类方法签名完整性的前提下,提供了额外的功能。

    我们通过下面的实例来演示装饰器模式的用法。其中,我们将把一个形状装饰上不同的颜色,同时又不改变形状类。

    参考链接: https://www.runoob.com/design-pattern/decorator-pattern.html

    代码案例

    新增车子接口

    /**
     * 生产车子接口
     */
    public interface Cars {
        String product();
    }

    三个实现类

    自行车

    /**
     * 生产自行车
     */
    @Slf4j
    public class   Bicycle implements Cars {
        @Override
        public String product() {
            log.info("生产自行车");
            return "自行车";
        }
    }

    汽车

    /**
     * 生产汽车
     */
    @Slf4j
    public class Car implements Cars {
        @Override
        public String product() {
            log.info("生产汽车");
            return "汽车";
        }
    }

    车子装饰器

    /**
     * 车子装饰类
     */
    @Slf4j
    public abstract class CarsDecorator implements Cars {
        protected Cars decoratorCars;
    
        public CarsDecorator(Cars decoratorCars) {
            this.decoratorCars = decoratorCars;
        }
    
        @Override
        public String product() {
            return decoratorCars.product();
        }
    }

    颜色装饰器

    /**
     * 土豪金色车子装饰器
     */
    public class GoldenCarsDecorator extends CarsDecorator {
        public GoldenCarsDecorator(Cars decoratorCars) {
            super(decoratorCars);
        }
    
        @Override
        public String product() {
            String product = decoratorCars.product();
            return setColour(decoratorCars)+"----"+product;
        }
    
        public String setColour(Cars decoratorCars){
            decoratorCars.product();
            return "土豪金色";
        }
    }

    测试主类

    /**
     * 设计模式控制器
     */
    @RestController
    @RequestMapping("/designPattern")
    @Slf4j
    public class DesignController {
    
        @GetMapping("/decorator")
        public ResponseModel decorator() {
            com.koukay.student.design.decorator.Cars bicycle= new com.koukay.student.design.decorator.impl.Bicycle();
            CarsDecorator cdCar = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Car());
            CarsDecorator cdBicycle = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Bicycle());
            com.koukay.student.design.decorator.Cars cdCarCars = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Car());
            com.koukay.student.design.decorator.Cars cdBicycleCars = new GoldenCarsDecorator(new com.koukay.student.design.decorator.impl.Bicycle());
            String bicyclePS = bicycle.product();
            String CarD = cdCar.product();
            String bicycleD = cdBicycle.product();
            String carP = cdCarCars.product();
            String bicycleP = cdBicycleCars.product();
            LinkedHashMap map= new LinkedHashMap();
            map.put("bicyclePS",bicyclePS);
            map.put("CarD",CarD);
            map.put("bicycleD",bicycleD);
            map.put("carP",carP);
            map.put("bicycleP",bicycleP);
            return new ResponseModel("装饰器模式完成", 200, map);
        }
    }

    测试案例

    2022-06-25 01:05:47.315 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:132022-06-25 01:05:47.316 INFO  生产汽车 【http-nio-8081-exec-7】【Car:132022-06-25 01:05:47.317 INFO  生产汽车 【http-nio-8081-exec-7】【Car:132022-06-25 01:05:47.326 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:132022-06-25 01:05:47.328 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:132022-06-25 01:05:47.328 INFO  生产汽车 【http-nio-8081-exec-7】【Car:132022-06-25 01:05:47.328 INFO  生产汽车 【http-nio-8081-exec-7】【Car:132022-06-25 01:05:47.329 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:132022-06-25 01:05:47.329 INFO  生产自行车 【http-nio-8081-exec-7】【Bicycle:13】
  • 相关阅读:
    Less的用法
    有关cookie的内容
    有关BOM(Browser Object Model)的内容
    JSON的相关内容
    css3实现菜单栏选中时的过渡效果
    原生JavaScript实现jQuery的hasClass,removeClass,addClass,toggleClass
    JavaScript实现多个菜单的显示隐藏(JavaScript实现二级/三级菜单的显示隐藏)
    用css控制table td内文字超出隐藏
    Centos查看端口占用令
    mysql 远程访问不行解决方法 Host is not allowed to connect to this MySQL server
  • 原文地址:https://www.cnblogs.com/hikoukay/p/16410620.html
Copyright © 2020-2023  润新知