1

From Design Pattern by Gang of Four

Why does the reference component of the decorator Decorator to the decorated point to the interface Component of the decorated,

enter image description here

while the reference realSubject of the proxy Proxy point to the concrete RealSubject?

enter image description here

Thanks.

Tim
  • 1
  • 141
  • 372
  • 590
  • Because that's the way the GoF conceived them, really. They define several groups of similar patterns, largely, it seems, because they showcase variations on the book's underlying themes of programming to interfaces and favoring composition over inheritance of implementation. The GoF does ascribe different purposes to these two patterns, and you could interpret the differences in light of those purposes, but focusing there risks missing the underlying themes, which are much more important than the specific patterns presented. – John Bollinger Oct 01 '17 at 21:57
  • @John Bollinger that is not a correct explanation . Decorator is about attaching ***additional*** responsibilities ***dynamically***. It also says - _"...providing different Decorator classes for a specific Component class lets you mix and match responsibilities. Decorators also make it easy to add a property twice..."_. To maintain the capability of adding decorator to Concrete Component as well as any other decorator (may be even same decorator twice), Decorator must have reference of base type of these both types and Component is the only place where this is true. – Kedar Tokekar Oct 03 '17 at 13:44
  • Decorator is about adding additional responsibilities in a layered manner and is also known as _Wrapper_ (Decorators can decorate decorated objects as well). where as Proxy is a surrogate object to to control access to RealSubject. basically intents are different. Proxy is not generic for Hierarchy but it is specific to Real Subject. – Kedar Tokekar Oct 03 '17 at 13:55
  • As I said, @KedarTokekar, "The GoF does ascribe different purposes to these two patterns, and you could interpret the differences in light of those purposes." It's your business if you prefer to look at it in those terms, but that doesn't change the fact that doing so disregards the deeper message. – John Bollinger Oct 03 '17 at 14:15

1 Answers1

1

Proxy may point exclusively to its subject's interface. The GoF mentions,

Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same.

But depending on implementation, a Proxy may instantiate its subject by calling a constructor, which necessitates referencing a concrete object.

jaco0646
  • 15,303
  • 7
  • 59
  • 83
  • Thanks. "a Proxy may instantiate its subject by calling a constructor, which necessitates referencing a concrete object." May a decorator have the same necessity? – Tim Jun 11 '19 at 21:35
  • A Decorator would not instantiate its wrapped component, so there should not be any concrete dependency. – jaco0646 Jun 11 '19 at 23:57
  • What instantiates the wrapped component in a decorator? – Tim Jun 12 '19 at 00:00
  • The Decorator doesn't care where its component comes from. – jaco0646 Jun 12 '19 at 00:36
  • Does a proxy care where its component comes from? when yes and when no? – Tim Jun 12 '19 at 00:48
  • That is another way of asking the original question, so my original answer is still relevant. – jaco0646 Jun 12 '19 at 02:54