《Java极简设计模式》第07章:装饰模式(Decorator)

作者:冰河

星球:http://m6z.cn/6aeFbs

博客:https://binghe.gitcode.host

文章汇总:https://binghe.gitcode.host/md/all/all.html

源码地址:https://github.com/binghe001/java-simple-design-patterns/tree/master/java-simple-design-decorator

沉淀,成长,突破,帮助他人,成就自我。

  • 本章难度:★★☆☆☆
  • 本章重点:用最简短的篇幅介绍装饰模式最核心的知识,理解装饰模式的设计精髓,并能够灵活运用到实际项目中,编写可维护的代码。

大家好,我是冰河~~

今天给大家介绍《Java极简设计模式》的第07章,装饰模式(Decorator),用最简短的篇幅讲述设计模式最核心的知识,好了,开始今天的内容。

一、概述

动态地给一个对象添加一些额外的职责。就增加功能来说,Decorator模式相比生成子类更为灵活。

二、适用性

1.在不影响其他对象的情况下,以动态、透明的方式给单个对象添加职责。

2.处理那些可以撤消的职责。

3.当不能采用生成子类的方法进行扩充时。

三、参与者

1.Component 定义一个对象接口,可以给这些对象动态地添加职责。

2.ConcreteComponent 定义一个对象,可以给这个对象添加一些职责。

3.Decorator 维持一个指向Component对象的指针,并定义一个与Component接口一致的接口。

4.ConcreteDecorator 向组件添加职责。

四、类图

五、示例

Component

java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Component接口Person
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public interface Person {
    void eat();
}

ConcreteComponent

java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Person接口的实现类Man
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class Man implements Person{
    @Override
    public void eat() {
        System.out.println("男人在吃");
    }
}

Decorator

java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Decorator抽象类实现Person接口
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public abstract class Decorator implements Person{

    protected Person person;

    public void setPerson(Person person) {
        this.person = person;
    }

    public void eat() {
        person.eat();
    }
}

ConcreteDecorator

java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Decorator的子类
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class ManDecoratorA extends Decorator{
    @Override
    public void eat() {
        super.eat();
        reEat();
        System.out.println("ManDecoratorA类");
    }
    public void reEat() {
        System.out.println("再吃一顿饭");
    }
}
java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description Decorator的子类
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class ManDecoratorB extends Decorator{
    @Override
    public void eat() {
        super.eat();
        System.out.println("===============");
        System.out.println("ManDecoratorB类");
    }
}

Test

java 复制代码
/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description 测试类
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
public class Test {

    public static void main(String[] args) {
        Man man = new Man();
        ManDecoratorA md1 = new ManDecoratorA();
        ManDecoratorB md2 = new ManDecoratorB();

        md1.setPerson(man);
        md2.setPerson(md1);
        md2.eat();
    }
}

Result

bash 复制代码
男人在吃
再吃一顿饭
ManDecoratorA类
===============
ManDecoratorB类

好了,今天就到这儿吧,相信大家对装饰模式有了更清晰的了解,我是冰河,我们下期见~~

相关推荐
JZC_xiaozhong1 分钟前
多系统权限标准不统一?企业如何实现跨平台统一权限管控
java·大数据·微服务·数据集成与应用集成·iam系统·权限治理·统一权限管理
阿猿收手吧!11 分钟前
【C++】引用类型全解析:左值、右值与万能引用
开发语言·c++
「QT(C++)开发工程师」16 分钟前
C++ 策略模式
开发语言·c++·策略模式
iFeng的小屋27 分钟前
【2026最新当当网爬虫分享】用Python爬取千本日本相关图书,自动分析价格分布!
开发语言·爬虫·python
yugi98783828 分钟前
基于MATLAB的一键式EMD、EEMD、CEEMD和SSA信号去噪实现
开发语言·matlab·信号去噪
热爱编程的小刘1 小时前
Lesson05&6 --- C&C++内存管理&模板初阶
开发语言·c++
爬山算法1 小时前
Hibernate(85)如何在持续集成/持续部署(CI/CD)中使用Hibernate?
java·ci/cd·hibernate
菜鸟233号1 小时前
力扣647 回文子串 java实现
java·数据结构·leetcode·动态规划
qq_12498707532 小时前
基于Java Web的城市花园小区维修管理系统的设计与实现(源码+论文+部署+安装)
java·开发语言·前端·spring boot·spring·毕业设计·计算机毕业设计
h7ml2 小时前
查券返利机器人的OCR识别集成:Java Tesseract+OpenCV优化图片验证码的自动解析方案
java·机器人·ocr