0

According to Structural Patterns, they focus on how classes and objects are composed to form larger structures.

Adapter and Decorator fit this definition. But for proxy it's just an interaction between the subject and the real object, it's not combining the anything to give a new object. Shouldn't it be categorized as behavioral Pattern?

I have read different Post and also related StackOverflow Question like Why is proxy pattern Structural Pattern and why is State Pattern behavioral pattern? and Why decorator is a structural and not behavioral design pattern?

But none answer the Question.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
Gautam Naik
  • 109
  • 2
  • 11

1 Answers1

0

Interesting question, i will try to answer:

Behavioral patterns are focused on loosely coupled communication between objects and assignment of concrete responsibilities (as mentioned in links You provided). If we look at strategy pattern, template pattern or observer, we can distinct responsibilities and what every class does (what are their responsibilities and concrete implementations). If we swap one concrete implementation of strategy or observer on another one, communication still stays the same but swapping our object affects the way our object works (internally). In other way: in behavioral patterns we SWAP concrete implementation of classes to force the change behavior of fragment of the code without breaking general communication between objects. We don't focus here on relationships between objects, just swapping

In structural patterns (I look at decorator here, it may not be fully compatible with facade pattern where we focus to simplify number of interfaces):

  1. we focus on ADDING additional behavior to our wrapped object WITHOUT changing the behavior of the wrapped object itself. In behavioral patterns we would change concrete implementation of wrapped object and hide it behind, for example, interface. Here we don't change the concrete implementation of our wrapped object (it stays still same, it is still an instance of same concrete class), we just add/extends additional things/functionalities/behavior. Decorator and Proxy don't change/swap wrapped objects, they just add its own extra functionality (open-closed composition).

  2. we focus additionally on relationship between our wrappers. Decorator, proxy and adapter has normally (the simplest) one-to-one relationship between them and wrapped object whereas flyweight manages a bit more objects. In behavioral pattern we don't focus on number of elements to manage, the flow and flexibility of swapping elements is more important

Looking from this point of view it seems proxy doesn't fit to behavioral patterns. We can't change one proxy for another (because of incompatibility), it doesn't have also defined standard way of communication with the object which proxy wraps (in the contrary to mediator, observer or template pattern)