Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why decorator is a structural and not behavioral design pattern?

I consider myself having intermediate knowledge of GoF design patterns. However, I get confused when it comes to classifying those patterns into structural and behavioral patterns. I do not have any confusion about creational patterns.

From wikipedia - Decorator Pattern - In object-oriented programming, the decorator pattern is a design pattern that allows behavior to be added to an individual object, either statically or dynamically, without affecting the behavior of other objects from the same class.

From the above definition it is clear that it is about behavior, then

  1. Why decorator is a structural pattern?
  2. What is the criteria of a structural pattern?
  3. What is the criteria of a behavioral pattern?

Thanks.

like image 807
Anand Patel Avatar asked Apr 09 '13 15:04

Anand Patel


2 Answers

Behavioral patterns are about communication between separate objects: things like mediator, observer, chain of responsibility (even visitor, which is described as "separating an algorithm from the object structure on which it operates"). They specify how separate objects interact.

Structural patterns are about ways to compose and layer abstractions, they are things like adapter, bridge, and composite. Decorator is a technique for composing functionality, so it goes with the structural patterns.

like image 159
Nathan Hughes Avatar answered Oct 31 '22 21:10

Nathan Hughes


If you need an answer in terms of your quote: The change in behavior is due to structural modification.
I.e. you "decorate" an object (via specific structuring of your class) to achieve the required behavior (via delegation)

like image 22
Cratylus Avatar answered Oct 31 '22 21:10

Cratylus