I am currently learning the proxy pattern and have found two different implementations of it in two different books. Please see the links for patterns UML Diagrams as I cannot embed the pictures yet.
My teacher says that Pattern 1 is the correct implementation, although most of the tutorials and sources use the Pattern 2 implementation. My teacher argues that Pattern 2 is wrong because "It allows the proxy to call all public methods on the RealSubject, which makes it not really different from the adapter pattern", while Pattern 1 "enforces that the proxy only can call the functions from the interface class inside the real subject"
My questions are:
Thank you for your answers.
The difference between the two diagrams is that in #1 the Proxy references the Subject interface, whereas in #2 the Proxy references the concrete Subject implementation (a.k.a. RealSubject).
Both of these diagrams are correct, because the Gang of Four Proxy pattern covers a number of different use cases: on page 208 they list remote proxy, virtual proxy, protection proxy, and smart reference as different implementations of the same pattern. The implementations vary to the extent that some of them are aware of their concrete subject and some are not.
This distinction between awareness or ignorance of the concrete subject often causes confusion about the differences between the Proxy and Decorator patterns. It should not confuse Proxy with Adapter; because in the Adapter pattern, the Client and Subject have entirely different interfaces (hence the need for an Adapter). Proxy (and Decorator) share a common interface with their Client and Subject.