Proxy#

The Proxy design pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. This pattern is useful when you want to add an additional layer of control over an object, such as lazy initialization, access control, logging, or caching.

Key Concepts

  1. Proxy: The proxy object, which controls access to the real object.

  2. Real Subject: The actual object that the proxy represents.

  3. Client: The object that interacts with the proxy.

### Types of Proxies

  • Virtual Proxy: Controls access to a resource that is expensive to create.

  • Remote Proxy: Represents an object in a different address space.

  • Protection Proxy: Controls access to the original object based on access rights.

  • Smart Proxy: Adds additional behavior when an object is accessed.

UML Diagram#

+---------+        +-----------------+
|  Client | --->>  |    Subject      |
+---------+        +-----------------+
                         ^
                         |
                         |
+---------+        +-----------------+
|  Proxy  |------->| Real Subject    |
+---------+        +-----------------+`
--- Java Example ---
--- Python Example ---