Structural pattern

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.

Proxy design pattern structure
Proxy design pattern structure

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.
Proxy pattern sequence diagram
Proxy pattern sequence diagram
  1. The client sends a request for an object to the Factory .
  2. The Factory creates a Proxy for encapsulating the Object.
  3. The client executes the Proxy which was created by the Factory.
  4. The Proxy performs one or more actions before the execution of the Object.
  5. The Proxy hands over the execution to the Object.
  6. The Proxy performs one or more actions after the execution of the Object.
  7. The Proxy returns the result.

Real-world example

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.

Discover how the Proxy pattern can help us solve this problem.
Discover how the Proxy pattern can help us solve this problem.

About this book

Introduction to design patterns

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
All rights reserved ©
Reactive programming

© 2021, Copyright - Oscar Blancarte. All rights reserved.