美文网首页软件工程师
设计模式之装饰器模式(Decorator Pattern)

设计模式之装饰器模式(Decorator Pattern)

作者: 杨斌_1024 | 来源:发表于2020-05-12 21:07 被阅读0次

概述

The decorator pattern allows to add functionality to an object at run-time without altering its structure.

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

很直白了。也就是说装饰器负责给某个对象增加一些功能。

本文类图

本文类图

代码讲解

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

ConcreteCompoment

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

Decorator

具体的装饰类:ConcreteDecorator1

ConcreteDecorator1

测试结果

DecoratorPatternTestDemo

应用场景

应用场景 源码

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

本文代码GitHub地址

相关文章

网友评论

    本文标题:设计模式之装饰器模式(Decorator Pattern)

    本文链接:https://www.haomeiwen.com/subject/arbinhtx.html