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

1. 装饰者模式的介绍

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

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

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

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

2. 装饰者模式的类图

在这里插入图片描述

3. 装饰者模式的实现

3.1 创建一个抽象组建Chef

package blog;

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

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

package blog;

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

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

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

3.3 创建抽象装饰器ChefDecorator

package blog;

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

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

3.4 创建具体装饰器PlateChefDecorator

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 测试

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();

    }
}

相关推荐

  1. 装饰模式设计模式

    2024-05-02 13:26:02       3 阅读
  2. 装饰设计模式总结

    2024-05-02 13:26:02       35 阅读
  3. 设计模式装饰模式

    2024-05-02 13:26:02       38 阅读
  4. 设计模式装饰模式

    2024-05-02 13:26:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-02 13:26:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-02 13:26:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-02 13:26:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-02 13:26:02       20 阅读

热门阅读

  1. Android 修改Camera的最大变焦倍数

    2024-05-02 13:26:02       15 阅读
  2. 三生随记——午夜医院的诡异回声

    2024-05-02 13:26:02       11 阅读
  3. 美国国防部数据网格参考架构概述(下)

    2024-05-02 13:26:02       11 阅读
  4. 文件上传知识

    2024-05-02 13:26:02       11 阅读
  5. k8s面试29连问

    2024-05-02 13:26:02       10 阅读
  6. solidity(16)

    2024-05-02 13:26:02       14 阅读
  7. 【刷爆力扣之二叉树】107. 二叉树的层序遍历 II

    2024-05-02 13:26:02       14 阅读
  8. LeetCode //C - 44. Wildcard Matching

    2024-05-02 13:26:02       12 阅读
  9. SQLServer聚合函数

    2024-05-02 13:26:02       11 阅读