设计模式(十):装饰者模式

设计模式(十):装饰者模式

  • [1. 装饰者模式的介绍](#1. 装饰者模式的介绍)
  • [2. 装饰者模式的类图](#2. 装饰者模式的类图)
  • [3. 装饰者模式的实现](#3. 装饰者模式的实现)
    • [3.1 创建一个抽象组建Chef](#3.1 创建一个抽象组建Chef)
    • [3.2 创建两个具体组件ChineseChef和EuropeanChef](#3.2 创建两个具体组件ChineseChef和EuropeanChef)
    • [3.3 创建抽象装饰器ChefDecorator](#3.3 创建抽象装饰器ChefDecorator)
    • [3.4 创建具体装饰器PlateChefDecorator](#3.4 创建具体装饰器PlateChefDecorator)
    • [3.5 测试](#3.5 测试)

1. 装饰者模式的介绍

装饰器模式(Decorator Pattern)属于结构型模式,允许向一个现有的对象添加新的功能,同时又不改变其结构。

装饰器模式通过将对象包装在装饰器类中,以便动态地修改其行为。

装饰器模式包含四个核心角色:

  • 抽象组件(Component):定义了原始对象和装饰器对象的公共接口或抽象类,可以是具体组件类的父类或接口。
  • 具体组件(Concrete Component):是被装饰的原始对象,它定义了需要添加新功能的对象。
  • 抽象装饰器(Decorator):继承自抽象组件,它包含了一个抽象组件对象,同时可以通过组合方式持有其他装饰器对象。
  • 具体装饰器(Concrete Decorator):实现了抽象装饰器的接口,负责向抽象组件添加新的功能。具体装饰器通常会在调用原始对象的方法之前或之后执行自己的操作。

2. 装饰者模式的类图

3. 装饰者模式的实现

3.1 创建一个抽象组建Chef

java 复制代码
package blog;

/**
 * 厨师
 */
public interface Chef {
    void cook();
}

3.2 创建两个具体组件ChineseChef和EuropeanChef

java 复制代码
package blog;

/**
 * 中国厨师
 */
public class ChineseChef implements Chef {

    @Override
    public void cook() {
        System.out.println("制作中餐");
    }
}
java 复制代码
package blog;

/**
 * 西方厨师
 */
public class EuropeanChef implements Chef{
    @Override
    public void cook() {
        System.out.println("制作西餐");
    }
}

3.3 创建抽象装饰器ChefDecorator

java 复制代码
package blog;

/**
 * 厨师装饰器
 */
public abstract class ChefDecorator implements Chef {
    protected Chef chef;

    public ChefDecorator(Chef chef) {
        this.chef = chef;
    }
}

3.4 创建具体装饰器PlateChefDecorator

java 复制代码
package blog;

/**
 * 厨师摆盘装饰器
 */
public class PlateChefDecorator extends ChefDecorator{
    public PlateChefDecorator(Chef chef) {
        super(chef);
    }

    @Override
    public void cook() {
        chef.cook();
        plate();
    }

    private void plate() {
        if (chef instanceof ChineseChef) {
            System.out.println("中式摆盘");
        } else {
            System.out.println("西式摆盘");
        }
    }
}

3.5 测试

java 复制代码
package blog;

public class DecoratorDemo {
    public static void main(String[] args) {
        ChineseChef chineseChef = new ChineseChef();
        PlateChefDecorator plateChineseChef = new PlateChefDecorator(chineseChef);
        plateChineseChef.cook();

        EuropeanChef europeanChef = new EuropeanChef();
        PlateChefDecorator plateEuropeanChef = new PlateChefDecorator(europeanChef);
        plateEuropeanChef.cook();

    }
}
相关推荐
子非鱼a4 分钟前
【WEB】[SWPU2019]Web1
java·服务器·前端
花生了什么事o9 分钟前
自定义Spring Boot Starter全流程
java·spring boot·后端
似水এ᭄往昔12 分钟前
【Qt】--常用控件(显示类控件)
开发语言·qt
PHP实战开发录22 分钟前
PHP文件上传成功为什么页面却找不到
开发语言·nginx·php·开发
torpidcat1 小时前
ruoyi-vue-pro 若依芋道 java springboot +mybatis 生日查询相关
java·vue.js·spring boot
AC赳赳老秦1 小时前
文旅市场公开数据分析:基于 OpenClaw 采集景区客流与门票公示数据,生成区域文旅热度监测报告
java·c语言·python·php·symfony·deepseek·openclaw
云小逸2 小时前
C++ 第一阶段:对象、内存与生命周期
开发语言·c++
玖玥拾2 小时前
Lua 基础语法(二)
开发语言·unity·lua
王的宝库2 小时前
GO常用标准库包
开发语言·后端·golang
geovindu2 小时前
python: Face Recognition
开发语言·后端·python·人脸识别