问题描述
我从维基阅读了装饰设计模式,以及 http://www.vincehuston.org/dp/decorator的代码示例
I've read the decorator design pattern from wiki http://en.wikipedia.org/wiki/Decorator_pattern, and code example from http://www.vincehuston.org/dp/decorator.html.
我看到传统继承遵循is-a模式的观点,而装饰器遵循has-a模式。而装饰师的召唤约会看起来像皮肤超过'皮肤'。例如
I see the point that traditional inheritance follows an 'is-a' pattern whereas decorator follows a 'has-a' pattern. And the calling convention of decorator looks like a 'skin' over 'skin' .. over 'core'. e.g.
I* anXYZ = new Z( new Y( new X( new A ) ) );
如上述代码示例链接所示。
as demonstrated in above code example link.
然而,仍然有几个我不明白的问题:
However there are still a couple of questions that i do not understand:
-
维基是什么意思?可以用于在运行时扩展(装饰)特定对象的功能? 'new ...(new ...(new ...))'是一个运行时调用,并且很好,但AwithXYZ anXYZ是编译时的继承,是坏的?
what does wiki mean by 'The decorator pattern can be used to extend (decorate) the functionality of a certain object at run-time'? the 'new ...(new... (new...))' is a run-time call and is good but a 'AwithXYZ anXYZ;' is a inheritance at compile time and is bad?
从代码示例链接我可以看到,类定义的数量在两个实现中几乎相同。我记得在其他一些设计模式书,如头第一设计模式。他们使用starbuzz咖啡作为例子,并说传统的继承会导致每个咖啡组合的一个'类爆炸',你会想出一个类。但在这种情况下,装饰器不一样吗?如果装饰器类可以使用任何抽象类并对其进行装饰,那么我猜这样做可以防止爆炸,但是从代码示例中,您可以确切地确定类的定义,而不是...
from the code example link i can see that the number of class definition is almost the same in both implementations. I recall in some other design pattern books like 'Head first design patterns'. They use starbuzz coffee as example and say traditional inheritance will cause a 'class explosion' 'cause for each combination of coffee, you would come up with a class for it. But isn't it the same for decorator in this case? If a decorator class can take ANY abstract class and decorate it, then i guess it does prevent explosion, but from the code example, you have exact # of class definitions, no less...
有人可以帮忙解释吗?
非常感谢,
推荐答案
我们以一些抽象的流为例,想象你想要提供加密和压缩服务。
Let's take some abstract streams for example and imagine you want to provide encryption and compression services over them.
使用装饰器(伪代码):
With decorator you have (pseudo code):
Stream plain = Stream();
Stream encrypted = EncryptedStream(Stream());
Stream zipped = ZippedStream(Stream());
Stream zippedEncrypted = ZippedStream(EncryptedStream(Stream());
Stream encryptedZipped = EncryptedStream(ZippedStream(Stream());
继承,你有:
class Stream() {...}
class EncryptedStream() : Stream {...}
class ZippedStream() : Stream {...}
class ZippedEncryptedStream() : EncryptedStream {...}
class EncryptedZippedStream() : ZippedStream {...}
1)根据你的需要,每个类只处理功能的一个方面(压缩,加密,...)
1) with decorator, you combine the functionality at runtime, depending on your needs. Each class only takes care of one facet of functionality (compression, encryption, ...)
2)在这个简单的例子中,我们有3带有装饰的课程,5与继承。现在我们再添加一些服务,例如过滤和剪辑。使用装饰器,您只需要2个课程来支持所有可能的场景,例如过滤 - >剪切 - >压缩 - > encription。
通过继承,您需要为每个组合提供一个类,以便最终得到数十个类。
2) in this simple example, we have 3 classes with decorators, and 5 with inheritance. Now let's add some more services, e.g. filtering and clipping. With decorator you need just 2 more classes to support all possible scenarios, e.g. filtering -> clipping -> compression -> encription.With inheritance, you need to provide a class for each combination so you end up with tens of classes.
这篇关于装饰设计模式与遗产?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!