Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

README.md

Proxy Pattern

Intent

The Proxy Pattern is a structural design pattern that provides an object representing another object. It acts as an intermediary to control access to the real object, often adding functionality like lazy loading, caching, access control, or logging, while maintaining the same interface as the real object.

Problem

In many scenarios, directly accessing an object can be inefficient or impractical. This can happen if:

  • The object is expensive to create or initialize.
  • The object requires additional functionality such as access control or monitoring.
  • The object needs to be accessed remotely (e.g., via a network call), which could be costly in terms of performance.

Creating a proxy object can help manage access to the real object in a controlled way.

Applicability

Use the Proxy Pattern when:

  • You need to control access to an object.
  • You want to add additional functionality to an object (e.g., caching, logging) without modifying the real object.
  • You want to delay the instantiation of a real object until it is actually needed (Lazy Initialization).
  • You want to add an additional layer of security (Access Control).

How to Implement

  1. Define the Subject Interface: Define a common interface that both the real object and proxy will implement. This ensures that the proxy can be used in place of the real object without altering the client code.

  2. Create the RealSubject: The real object that performs the core functionality. It implements the Subject interface.

  3. Create the Proxy: The proxy class also implements the Subject interface. It holds a reference to the real object and delegates calls to it, potentially adding additional functionality like caching or access control.

  4. Lazy Initialization (Optional): The proxy can instantiate the real object only when it's required, saving resources.

  5. Use the Proxy: The client interacts with the proxy object, which will manage access to the real object.

Pros

  • Reduced Resource Usage: Delays the creation of the real object until it's actually needed (Lazy Initialization).
  • Additional Functionality: Allows you to add extra functionality (e.g., caching, logging, access control) without modifying the real object.
  • Access Control: The proxy can control access to the real object, adding security or validation.
  • Performance Optimization: Can be used for optimization, such as caching expensive results or reducing the number of resource-heavy operations.

Cons

  • Increased Complexity: Adding proxy objects introduces additional layers of abstraction and complexity.
  • Performance Overhead: There might be minor overhead due to the indirection of the proxy object.
  • Maintenance: Managing proxies and their configurations can become difficult if overused or not properly structured.

When to Use

Use the Proxy Pattern when:

  • You need to add functionality (e.g., caching, logging, access control) to an existing object without modifying it.
  • You want to control access to expensive or resource-heavy objects.
  • Lazy initialization or delayed object creation is required for performance reasons.
  • You need to implement remote access to an object (e.g., network proxy).

When Not to Use

Avoid using the Proxy Pattern when:

  • The object is lightweight and creating a new instance is not resource-intensive.
  • The added complexity of managing proxies outweighs the benefits.
  • You do not need to control access to the object or add additional functionality.

Real-World Use Cases for the Proxy Pattern

  1. Caching Proxy: In a caching proxy, the proxy stores the result of expensive computations or API calls. The next time the client requests the same data, the proxy returns the cached result instead of calling the real object again.

  2. Virtual Proxy: This is used to delay the creation or initialization of a resource-heavy object. For example, an image viewer might load a high-resolution image only when it is actually needed.

  3. Remote Proxy: Used when accessing objects that exist in a different address space (e.g., objects on a different server). The proxy handles communication between the client and the real object over a network.

  4. Security Proxy: A proxy can be used to control access to a real object, ensuring that only authorized clients can access certain functionality or data.

  5. Protection Proxy: Provides additional security or access control, allowing only certain users or clients to interact with the real object.

Summary

  • The Proxy Pattern is ideal for adding an additional layer of control and functionality to objects without modifying them.
  • It can be used for various purposes, including lazy initialization, caching, remote access, and access control.
  • While the pattern provides many benefits, such as reducing resource consumption and improving performance, it also introduces additional complexity and potential overhead. Therefore, it's important to use proxies when they provide clear benefits in managing resources or controlling access to objects.