一、概念
装饰模式,动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更灵活。
二、UML类图

三、具体demo






三、总结
1.装饰模式是为已有功能动态添加更多功能的一种方式
2.它把每个要装饰的功能放在单独的类中,并让这个类包装它所需要装饰的对象,因此,当需要执行特殊行为时,客户代码就可以在运行时根据需要有选择地、按顺序地使用装饰功能包装对象了
3.优点:把类中的装饰功能从类中搬移去除,可以简化原有的类。有效地把类的核心职责和装饰功能分开了。而且可以去除相关类中重复的装饰逻辑。
网友评论