问题
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
- Why decorator is a structural pattern?
- What is the criteria of a structural pattern?
- What is the criteria of a behavioral pattern?
Thanks.
回答1:
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.
回答2:
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)
来源:https://stackoverflow.com/questions/15906563/why-decorator-is-a-structural-and-not-behavioral-design-pattern