Everything you just saw in this article is only a small part of the book Introduction to design patterns, the most complete book of design patterns in Spanish, we cover 25 design patterns along with 25 real-world projects. Forget about learning design patterns with typical Internet examples of how to make pizza, animals, and geometric shapes. I invite you to see my book:See book
This pattern is devoted to the mediation between two objects. What I refer as mediation is the series of actions executed before and after carrying out the task requested by the user. The main trait of the Proxy pattern is that it keeps the user unaware of the mediation that is being carried out in the background, because the client receives an object structured as expected, all by interacting, unknowingly, with a proxy.
The components included in the pattern are:
- IObject: It represents the common interface between the Object and the Proxy.
- Object: It represents the actual object the client wants access to.
- Proxy: Class which implements IObject and is the virtual representation of the Object.
- The client sends a request for an object to the Factory .
- The Factory creates a Proxy for encapsulating the Object.
- The client executes the Proxy which was created by the Factory.
- The Proxy performs one or more actions before the execution of the Object.
- The Proxy hands over the execution to the Object.
- The Proxy performs one or more actions after the execution of the Object.
- The Proxy returns the result.
By implementing the Proxy design pattern, we are going to create a security mechanism that intercepts processes executions to check if the user attempting to trigger them has the required privileges, in order to restrict unauthorized executions. Furthermore, each executed process will be audited and registered. All of this will happen without the user noticing, because the proxy would encapsulate the security logic.