Reputation: 1630
Why is decorator a structural pattern and chain of responsibility isn't?
The only difference between these two is that one passes along no matter what, while the other will be handled by exactly one object.
How does this difference make one a structural pattern and the other behavioral pattern?
Upvotes: 0
Views: 655
Reputation: 1630
Seems like structural pattern is the miscellaneous category of design patterns. So the question is really about why is Chain of Responsibility a behavioral pattern but decorator isn't.
Chain of Responsibility and decorator are different in one way, decorator always goes through all the objects in the chain, while Chain of Responsibility gets handled by one of the objects and stops the chain. That makes Chain of Responsibility a mechanism for choosing one behavior out of many. And decorator isn't about choosing any one behavior out of many, that makes it not a behavioral pattern. And since it isn't a creational pattern either, it has to be categorized as a structural pattern.
Upvotes: 2
Reputation: 2291
The "difference that one passes along no matter what, while the other will be handled by exactly one object" is not what determines whether Structural or Behavioral.
Structural patterns ( as per Gof)
are concerned with how clases and objects are composed to form larger structures. Structural patterns use inheritance to compose interfaces or implementations.
Behavioral patterns ( as per Gof)
are concerned with algorithms and the assignment of responsibilities between objects.
It does seem the distinction is academic in the case of Decorator vs Chain but the fact remains that Decorator complies with the definition of Structural.
Chain also uses linked lists but its not essential the list items are related by inheritance but the list is about assigning or taking responsibility.
That's the reply as per GoF, but I don't believe its worth the time for new student of patterns to waste too much thought on the distinction in this case, especially given the similarity of the end products!
Upvotes: 0