12.
El patrón decorador
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
Whoops, an error! Please, try again later.
5 Comments
Hi!
If I'm not mistaken, technically, it could be possible to decorate a class without it implementing an interface, making the decorator extend the class to be decorated.
Is this truly possible?
Since this is not the intended use of inheritance, would it be a bad practice to do so if we dont have an interface to implement from?
Thanks!
Hey @Oleguer-C
That's a good question., and yes, you can achieve basically the same behavior by using inheritance instead of composition in this case (but composition is better, it gives you more flexibility and reusability)
Cheers!
What is the difference with the decorator strategy of what we saw in OOP composition?
Hey theNaschkatze!
That's a cool question :). They are , sort of, the "same". Or, to be more precise, composition is kind of like a "tool": it's a tool that allows you to "extend" the behavior of an existing class without creating a sub-class. I think the key to turning this "composition" into the decorator pattern would be the step where you "replace" all of the instances of the original class with the new one. In practice, when you use composition, it probably very often (always) meant to power the decorator strategy. And the key difference , I believe, is that "composition" is a mechanical thing you can do with classes/interfaces, while the decorator strategy goes further to talk about what changes you'll now make in your system thanks to this new composition.
I hope that helps - very thought-provoking question actually :).
Cheers!
Thank and tanks for all the videos! I think it really level up my coding skills and I mainly do front-end. Not only the topics are good, but the scripts are really educative and fun at the same time!
"Houston: no signs of life"
Start the conversation!