-2

I do not understand why do I have to implement the interface in Proxy Pattern target object to represent. I think at it as the common use of an interface, which is implemented by proxy class, which will provide to get real object class info and show them to the client...but I see that all real object class examples implements the interface.....why?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
Noomak
  • 371
  • 5
  • 19

1 Answers1

3

The goal of the Proxy Pattern is to have an object (the Proxy) that can stand in for another one (the Real object). To achieve this, both objects (the Proxy and the Real object) must implement a common interface: the one used by the Client. That way, the Client will only have to manipulate the interface, no matter if it is implemented directly by the Real object, or indirectly by the Proxy forwarding to the Real object.

There's another, very close, pattern called the Adapter pattern, which doesn't require the Real object to implement this interface. This is because the goal is different: the Adapter aims to allow the Client to manipulate indirectly a Real object that does not implement the interface.

Arkanosis
  • 2,229
  • 1
  • 12
  • 18