Pius Adams Ijachi
3 min readJan 16, 2024
proxy pattern

PROXY DESIGN PATTERN

Hey there, welcome to my blog! Today I’m going to talk about the proxy pattern, what it is, when to use it, and how it differs from the decorator pattern. I’ll also show you a simple example of how to implement it in PHP.

What is the proxy pattern?

The proxy pattern is a structural design pattern that lets you provide a substitute or placeholder for another object. A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object.

Why use the proxy pattern?

The proxy pattern can be useful when you want to add some extra functionality or logic to an existing object without changing its interface or modifying its code. For example, you can use a proxy to:

  • Implement lazy loading: A proxy can defer the creation of a heavy object until it’s needed, saving memory and improving performance.
  • Implement caching: A proxy can cache the results of expensive operations and return them if the same inputs occur again, avoiding unnecessary computations.
  • Implement access control: A proxy can check the permissions of the clients before forwarding the request to the original object, protecting it from unauthorized access.
  • Implement logging: A proxy can keep a record of the requests and responses between the client and the original object, useful for debugging or monitoring purposes.

How does the proxy pattern differ from the decorator pattern?

The proxy pattern and the decorator pattern are both structural design patterns that allow you to wrap an object with another object with the same interface. However, they have different intents and purposes.

The decorator pattern dynamically adds new behaviors or responsibilities to an object, without affecting other objects that use it. The decorator acts as a transparent wrapper that forwards all requests to the original object and may perform additional actions before or after that.

The proxy pattern is used to control access to an object, without changing its interface or behavior. The proxy acts as a non-transparent wrapper that may or may not forward requests to the original object, depending on some conditions.

How to implement the proxy pattern in PHP?

To implement the proxy pattern in PHP, you need to define an interface that both the original object and the proxy will implement. This way, you can use them interchangeably. Then, you need to create a proxy class that will hold a reference to the original object and implement the same interface. The proxy class will delegate all requests to the original object, but may also perform some additional actions before or after that.

Let’s see an example of how to use the proxy pattern in PHP. Suppose we have a PaymentService class that provides a simple method for crediting an account:

class PaymentService {
    public function credit($amount) {
echo "Crediting $amount\n";
}
}

Now, let’s say we want to add some logging functionality to this method, without modifying the PaymentService class. We can create a PaymentProxy class that implements the same interface as PaymentService and holds a reference to it:

class PaymentProxy implements PaymentService {
    private $paymentService;    public function __construct(PaymentService $paymentService) {
$this->paymentService = $paymentService;
}
public function credit($amount) {
echo "Logging credit request: $amount\n";
$this->paymentService->credit($amount);
echo "Logging credit response: success\n";
}
}
Now, we can use the PaymentProxy class instead of the PaymentService class, and get the same functionality plus logging:$realPayment = new PaymentService();$realPayment->credit(200); // this is fine but assuming you want to add logging or some other logic to this method$proxyPayment = new PaymentProxy($realPayment);$proxyPayment->credit(200); // this will also log the request and response