I was going through the proxy pattern and noticed that the Proxy class also implements the Subject interface, which is implemented by the concrete implementations or the Subject classes as well.
Can anyone provide a reason why we need to do so ?
We could have created a function in the proxy class and call the subject methods inside this function. Then the client code can call this proxy class function and the appropriate methods could be called.
Following the way you suggested would have the same result but I would not call it a proxy, I would call it a wrapper. Using the proxy design pattern with an interface would be preferable IMHO, because the client could be agnostic of which class is actually used to execute the respective functionality. The client would only see the subject
interface class and not know anything about the Proxy
or the RealSubject
concrete classes. This would be important for long-term maintenance of the code.
Of course a design pattern is not something strict that you should follow in order to be "right". It is a guideline for common software engineering scenarios. Therefore, you should implement it in the way it is most convenient for you.
Just to avoid misunderstandings, I clarify that I used this as a reference for the proxy design pattern.