0

上面的解释非常好。

但是,我对 Decorator Pattern (DeP) 的实现感到有些困惑,如

http://www.netobjectives.com/resources/books/design-patterns-explained/java-code-examples/chapter17/#17-1

上述链接代码的设计在 tinypic.com/view.php?pic=xnaqlt&s=3

我对“super.callTrailer();”感到困惑 在装饰器类 Header1、Header2、Footer1 和 Footer2 中,都派生自 TicketDecorator。

不应该只是“callTrailer();”吗?? 由于“private Component myTrailer;”这一行,每个装饰器对象都有自己对下一个装饰器的引用。

注意:我不是很精通 Java,也是设计模式的初学者。

4

1 回答 1

2

They will both give you the same result. Since callTrailer() only exists in the parent class then calling callTrailer() by itself will automatically call the callTrailer() of the TicketDecorator. I guess the reason they put super in there is to make it explicit that it is calling the parent's method.

于 2010-07-17T16:32:59.380 回答