Imran
Imran

Reputation: 1

Confusion about super keyword; Decorator Pattern implemented in Java

the above explanation is very nice.

However, I am slightly confused by the implementation of Decorator Pattern (DeP) as given in

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

The design for above linked code is given at tinypic.com/view.php?pic=xnaqlt&s=3

I am confused by "super.callTrailer();" in the decorator classes Header1, Header2, Footer1 and Footer2, all derived from TicketDecorator.

Shouldn't it be just "callTrailer();" ? As each decorator object would have its own reference to the next decorator due to the line "private Component myTrailer;".

Note: I am not very well versed in Java and a beginner in Design Patterns.

Upvotes: 0

Views: 396

Answers (1)

Romain Hippeau
Romain Hippeau

Reputation: 24375

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.

Upvotes: 2

Related Questions