Moderatoren: pmueller, SE - Design and Construction
- Beiträge: 11
- Registriert: 25. Apr 2018 16:48
Which feature makes it possible to implement a new alternative of the Decorator design pattern in Scala? Shortly describe
this alternative by discussing the consequences.
My first answer to this question was the following one:
Traits with standard implementations for methods. A consequence would be that if subclasses of this trait already implement one of the added methods with the same signature, they lack the "override" keyword before the method, hence the compiler will complain.A further problem would be that these standard methods would have to work for all of the subclasses, as clients would be able to execute these methods on objects that were not intended to have these methods.
However, after googling the question I found this blog post
referring to the "abstract override" keyword.
Does someone have a valid answer to this question that he/she would like to discuss?
- Beiträge: 19
- Registriert: 29. Okt 2017 21:47
Disclaimer: I'll try to make a more educated guess after I figured out what exactly the 6.3-slides are trying to teach us. The UML diagram is a mess and doesn't make any sense at all.
My ad hoc idea would be mixin composition of traits. You could apply the strategy pattern within the decorator pattern as hinted on the slides which would yield a great design. I'd specify the Decorator as an interface and use the strategy pattern to enforce OCP and ISP compliant design via mixin traits. I have no idea if that is a valid answer of course.
The last slide of 6.3 also hints towards mixin composition via ""Static" Decorators". I haven't fully understood that concept yet either but that might be also an approach to an answer.
VSchü's answer is more educated than mine.
Zuletzt geändert von llllllll
am 12. Jul 2018 16:00, insgesamt 2-mal geändert.
- Beiträge: 26
- Registriert: 5. Jul 2015 16:46
You can refer to slide 25 of 6.3-DecoratorPattern. Essentially you can write something like
Code: Alles auswählen
class A extends Component
trait Decorator extends Compenent
val a = new A with Decorator
to create an object of class A and mixin trait Decorator (in other words, statically decorate A with Decorator). The main difference to the real decorator pattern is that this happens statically and you can only do this on object creation, so it's not possible to add Decorator to an existing object of A.
Edit: There are some further differences: Unlike in the real decorator pattern, the result is not an object of type A wrapped in an object of type Decorator, but an object of type "A with Decorator", so you don't end up with the typical "Layers". Furthermore you get "delegation semantics" as seen on slide 9 of the same slideset.
- Beiträge: 106
- Registriert: 20. Apr 2016 22:49
I would have said like this but I don't know which is correct.
feature : Mixins
Consequence: Static binding