This answer almost describes the first half of the question.
It says:
After reading the Gang of Four definitions, I'm not convinced there's a real difference. (included for convenience)
- Decorator: Allows for the dynamic wrapping of objects in order to modify their existing responsibilities and behaviours
- Chain of Responsibility: Gives more than one object an opportunity to handle a request by linking receiving objects together
Wikipedia fleshes them out a little, but some of it's kinda arbitrary.
Decorator is typically implemented as a Linked List. But I think that's too low-level to be considered "part" of the pattern.
Chain of Responsibility links only handle data if it's their responsibility; but determining responsibility and data handling are both part of behavior. Decorators can do this just as easily.
- Decorator requires you to call the delegate.
- A "pure" CoR link should only call the delegate if it doesn't handle the data. The first two attributes don't really distinguish the patterns.
The second two do, but the way Decorator and CoR are usually implemented don't enforce those attributes--the designer just hopes no one writes a Decorator that breaks the chain or a CoRLink that continues the chain after handling the data.
The structure of both patterns is almost identical. What forces the decorator pattern to be implemented this way? Instead of having a ConcreteElement
and some Decorator
s, what prevents me from just having each Decorator
pointing to the object it's wrapping and when the pointer to the object being wrapped is null
, then do the same as what happens in the ConcreteElement
? What makes each structure specific to its pattern?
Also, why are they in different categories? Furthermore, reading the other answers here, it looks like even though the structure is almost the same, the intent is different. The CoR
is intended for having couple of potential objects that may handle the request, and the object that will handle the request is not known in advance. Whereas the decorator is intended for wrapping an object and adding some functionality at runtime (why can't I do that with CoR?). Does it really make sense that the CoR
(which is intended for structuring several objects that are capable of processing the request together in a chain) to be behavioral
? Doesn't it seem like it should be structural
? Also, does it really make sence for the Decorator
(which is intended for encapsulating some behavior and attach it to some given object later) to be structural
? Doesn't it seem like it should be behavioral
?
Aucun commentaire:
Enregistrer un commentaire