Java设计模式之装饰者模式

Java设计模式相关文章

1. Java设计模式之模板模式
2. Java设计模式之策略模式
3. Java设计模式之工厂模式
4. Java设计模式之装饰者模式
5.Java设计模式之单例模式
Java 设计模式源代码,欢迎star
https://github.com/Dylan-haiji/design-pattern

概念

装饰(Decorator)模式的定义:指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。

优缺点

  • 主要优点有:
    a) 采用装饰模式扩展对象的功能比采用继承方式更加灵活。
    b) 可以设计出多个不同的具体装饰类,创造出多个不同行为的组合。
  • 主要缺点是:
    装饰模式增加了许多子类,如果过度使用会使程序变得很复杂。

代码演示

场景设定

装饰者开发介绍在这里 https://github.com/Dylan-haiji/design-pattern/tree/master/decoration-mode

结构图

在这里插入图片描述

3.1 定义业务接口

public interface ComputerService {

    /**
     * @Description 购买的电脑信息
     * @UserModule: design-pattern     
     * @author Dylan
     * @date 2020/2/3 0003
     * @param [type] 
     * @return com.javayh.dto.ComputerDTO
     */
    ComputerDTO getComputer(String type);
}

3.2 实现业务

public class ComputerServiceImpl implements ComputerService {

    @Override
    public ComputerDTO getComputer(String type) {
        return ComputerDTO.builder().brand(type).cpu(Constant.CPU)
                .graphicsCard("blackmagic eGPU Pro ")
                .nucleus("16")
                .size("17").build();
    }
}

3.3 定义装饰者

public class Decorator implements ComputerService {

    private ComputerService computerService;

    public Decorator(ComputerService computerService) {
        this.computerService = computerService;
    }
    @Override
    public ComputerDTO getComputer(String type) {
        ComputerDTO computer = computerService.getComputer(type);
        computer.setComputerGift(ComputerGift.builder()
                .member("恭喜您已成为本店铂金会员")
                .integral("赠送您10积分")
                .coupon("赠送您一张(30元)店铺统用优惠券").build());
        return computer;
    }
}

3.4 应用

    @GetMapping(value = "/getComputer/{type}")
    public DataResult getComputer(@PathVariable String  type){
        return DecoratorUtils.getType(type);
    }
======
工具

public class DecoratorUtils {
    
    public static DataResult getType(String type){
        Map<String,Integer> map = new HashMap();
        map.put(type,"Mac".equalsIgnoreCase(type)? 1 :("P30".equalsIgnoreCase(type)? 2 :3));
        ComputerService computerService = null;
        Decorator decorator = null;
        ComputerDTO computer = null;
        switch (map.get(type)){
            case 1:
                computerService = new ComputerServiceImpl();
                decorator = new Decorator(computerService);
                computer = decorator.getComputer(type);
                break;
            case 2:
                computerService = new PhoneServiceImpl();
                decorator = new Decorator(computerService);
                computer = decorator.getComputer(type);
                break;
            default:
                break;
        }
        return DataResult.success(computer);
    }
}

3.5 验证

模拟请求 http://localhost:8092/decoration/getComputer/MAC

在这里插入图片描述

关注 Java有货领取更多资料

联系小编。微信:372787553,带您进群互相学习
左侧小编微信,右侧获取免费资料
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小杨同学~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值