代码改变世界

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

2022-06-25 01:15  hikoukay  阅读(270)  评论(0编辑  收藏  举报

概念

装饰器模式(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】