装饰图案与分类 [英] Decorator pattern versus sub classing

查看:152
本文介绍了装饰图案与分类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以通过添加子类来解决添加功能的问题,然后为什么要使用装饰器模式装饰器模式的真正优势?

解决方案维基百科的装饰图案的div>


装饰器模式可以用于
使得可以在运行时扩展(装饰)
某个对象
的功能


装饰器模式的整个点是动态添加额外的行为/功能,这当然不可能在设计时间。



从同一篇文章:


装饰模式是一个
替代子类化。
子类在编译
时间
时添加行为,更改会影响原始类的所有
实例;
装饰可以为单个对象的
运行时提供新的行为



I can solve the problem of adding functionality by adding sub classing then why should I use decorator pattern what's the real advantage of decorator pattern ?

解决方案

from Decorator pattern at wikipedia

The decorator pattern can be used to make it possible to extend (decorate) the functionality of a certain object at runtime.

The whole point of decorator pattern is to dynamically add additional behaviour/functionality, which is of course not possible at design time.

from the same article:

The decorator pattern is an alternative to subclassing. Subclassing adds behavior at compile time, and the change affects all instances of the original class; decorating can provide new behavior at runtime for individual objects.

这篇关于装饰图案与分类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆