0
0
LldConceptBeginner · 3 min read

Proxy Pattern: Definition, Example, and When to Use

The Proxy Pattern is a design pattern that provides a placeholder or surrogate for another object to control access to it. It acts like a gatekeeper, managing requests to the real object and adding extra behavior without changing the original object's code.
⚙️

How It Works

Imagine you want to visit a friend’s house, but instead of going directly, you first meet their trusted assistant who checks if you have permission to enter. This assistant acts as a proxy for your friend, controlling access and possibly adding extra steps like logging or security checks.

In software, the Proxy Pattern creates a special object called a proxy that stands in place of the real object. When a client asks the proxy to do something, the proxy can decide whether to forward the request to the real object, block it, or add extra actions before or after the request.

This pattern helps manage complexity, improve security, or delay expensive operations by controlling how and when the real object is accessed.

💻

Example

This example shows a simple proxy controlling access to a resource. The proxy checks if the user is allowed before forwarding the request.

javascript
class RealResource {
    request() {
        return 'Real resource: Handling request.';
    }
}

class ProxyResource {
    constructor(user) {
        this.user = user;
        this.realResource = new RealResource();
    }

    request() {
        if (this.user === 'admin') {
            return this.realResource.request();
        } else {
            return 'Proxy: Access denied.';
        }
    }
}

const proxy1 = new ProxyResource('admin');
console.log(proxy1.request());

const proxy2 = new ProxyResource('guest');
console.log(proxy2.request());
Output
Real resource: Handling request. Proxy: Access denied.
🎯

When to Use

Use the Proxy Pattern when you want to control access to an object, add extra functionality, or delay expensive operations. Common real-world uses include:

  • Security: Check permissions before allowing access.
  • Lazy loading: Delay creating a heavy object until it’s really needed.
  • Logging: Track requests to an object without changing its code.
  • Remote proxies: Represent objects in different locations or networks.

This pattern helps keep your code clean and flexible by separating control logic from the main object.

Key Points

  • The proxy acts as a stand-in for another object.
  • It controls access and can add extra behavior.
  • Clients interact with the proxy as if it were the real object.
  • Useful for security, lazy loading, logging, and remote access.

Key Takeaways

The Proxy Pattern controls access to an object by using a surrogate.
It adds flexibility by allowing extra actions without changing the real object.
Use it for security checks, lazy loading, logging, or remote object access.
Clients use the proxy just like the real object, keeping code simple.
It helps separate concerns and manage complexity in software design.