Proxy Pattern

The Proxy design pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it. In other words, the Proxy pattern is all about controlling access to an object and adding some extra functionality while keeping the interface consistent.

Participants:

  • Subject: This is the interface that both the RealSubject (the actual object) and the Proxy implement. It defines the common operations that the RealSubject and the Proxy can perform.
  • RealSubject: This is the actual object that the Proxy represents. It implements the operations defined in the Subject interface.
  • Proxy: The Proxy class implements the same interface as the RealSubject. It holds a reference to the RealSubject and controls access to it. The Proxy can add additional behavior before or after invoking the RealSubject’s methods.

UML Class Diagram

classDiagram
  class Subject {
    +operation()
  }

  class RealSubject {
    -realOperation()
  }

  class Proxy {
    -realSubject: RealSubject
    +operation()
  }

  class Client {
    +request()
  }

  Subject <|-- RealSubject
  Subject <|-- Proxy
  Client --> Proxy
  Client --> Subject

  note for Proxy "Acts as intermediary\nand controls access"

Types of Proxies:

  • Virtual Proxy: Creates a placeholder object that stands in for the expensive-to-create RealSubject. The Virtual Proxy delays the creation of the RealSubject until it is actually needed.
  • Remote Proxy: Acts as a local representative for an object that resides in a different address space, often on a remote server. The Remote Proxy handles communication and marshalling/unmarshalling of data between the client and the remote object.
  • Protection Proxy: Controls access to a sensitive object by adding an extra layer of authorization or security checks. The Protection Proxy can restrict certain clients from accessing the RealSubject directly.
  • Cache Proxy: Stores the results of expensive operations and returns the cached result when the same operation is requested again. This can help improve performance by avoiding redundant calculations.
  • Logging Proxy: Logs method invocations and their parameters, providing a way to monitor and debug the behavior of the RealSubject. 9 Smart Reference Proxy: Performs additional actions when the RealSubject is accessed, such as counting references or releasing resources when they are no longer needed.

Examples

classDiagram
  
  class Image {
    +display()
  }

  class RealImage {
    -filename: String
    +loadImageFromDisk()
    +display()
  }

  class ProxyImage {
    -realImage: RealImage
    -filename: String
    +display()
  }

  Client --> ProxyImage
  Client --> Image
  ProxyImage <|-- RealImage
  Image <|-- RealImage

Implementation

Benefits of Using Proxy:

  • Control: The Proxy pattern allows you to control the access to an object, enforcing certain behavior or restrictions.
  • Lazy Loading: Virtual Proxies can delay the creation of resource-intensive objects until they are actually needed, improving performance.
  • Security: Protection Proxies can add security checks to control who can access certain objects and operations.
  • Caching: Cache Proxies can help reduce the computational load by storing and reusing previously computed results.