概述
The decorator pattern allows to add functionality to an object at run-time without altering its structure.
装饰器模式是指在不改变现有对象结构的情况下,动态地给该对象增加一些职责(即增加其额外功能)的模式,它属于对象结构型模式。
很直白了。也就是说装饰器负责给某个对象增加一些功能。
本文类图

代码讲解
ConcreteCompoment类是将要被装饰的具体类。装饰器把会额外执行一些行为,达到动态地给该类的对象增加一些职责。

装饰器的抽象类:Decorator,这类的作用是可以方便地创建多个具体装饰器。

具体的装饰类:ConcreteDecorator1

测试结果

应用场景


参考了装饰模式(装饰设计模式)详解 还有 装饰模式代码
网友评论