0

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?

Andy
  • 1,452
  • 1
  • 13
  • 18
  • A decorator pattern basically wraps an already existing class, and then adds some functionality to it. I guess this makes it structural. Chain of responsibility is all about having one implementation of a class delegate to another implementation, which is behavioral. – Tim Biegeleisen Aug 17 '18 at 01:48
  • @Tim Decorator pattern uses delegation too. https://stackoverflow.com/questions/13389544/difference-between-decorator-pattern-and-delegation-pattern – Andy Aug 17 '18 at 01:53
  • Based on the Wikipedia page for the decorator pattern, I did not see delegation being a primary role of the delegator pattern. – Tim Biegeleisen Aug 17 '18 at 01:54
  • @Tim I saw the word "delegation" in two places. For Chain of Responsibility page, I didn't see any. – Andy Aug 17 '18 at 01:59
  • Yes, delegation is precisely what chain of responsibility is all about. Maybe the decorator does delegate behaviors from the class it wraps, to that already existing class. – Tim Biegeleisen Aug 17 '18 at 02:00
  • I would suggest focusing on what design patterns actually _do_, rather than getting bogged down in minute details of the language used to describe them. – Tim Biegeleisen Aug 17 '18 at 02:04
  • @Tim There are some of us who want to know more than just what they do. – Andy Aug 17 '18 at 02:26
  • There are many design pattern which look quite similar. All are based on good design principle and hence share them. Design pattern are different from one another based on the *intent*. Decorator and the wrapped entity have a common (super) type like Text.java may be wrapped in EncodedText.java – nits.kk Aug 17 '18 at 05:19

2 Answers2

2

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.

Andy
  • 1,452
  • 1
  • 13
  • 18
0

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!

bcperth
  • 2,191
  • 1
  • 10
  • 16