I've recently been reading about design patterns using GOF's book on design patterns as well as Head First Design Patterns from O'REILLY. I noticed dissimilarities in the approach to Decorator pattern, namely: the placement of the decorated component reference.
The Head First book contains a structure diagram, where the reference to the decorated component is stored in each of the concrete decorator classes, whereas GOF places it a level higher, in the decorator abstract class.
The GOF version appels to me more - placing the reference in each of the concrete decorators seems like code repetition, since we might as well place it higher, in the decorator class (and it's not like a perticular conrete decorator won't be using it since it can't exist without something that it can decorate). This wouldn't be possible of course if the decorator class was declared as an interface and not an abstract class but on the other hand if we were to declare it as an interface its existence would be pretty much pointless since the concrete decorated classes could just implement the component interface (or at least that's what I imagine).
My question is: is there any advantage at all in doing it the Head First's way? Or am I perhaps wrong in any of my assumptions?
Aucun commentaire:
Enregistrer un commentaire