loader

Ever tried to get into an exclusive club and had to deal with a strict bouncer checking your credentials? That’s exactly what the Proxy Pattern does for your code. It acts as a gatekeeper, controlling access to a complex object, ensuring only the right requests get through—just like a bouncer at a VIP club.

Why Bother with It?

  • Controlled Access: Just as a bouncer only lets in the approved guests, a proxy restricts access to sensitive or resource-intensive operations.
  • Lazy Initialization: Sometimes, you don’t want to create a heavy object until it’s really needed. A proxy can delay this creation until the very moment it’s required.
  • Added Perks: Like a bouncer who not only checks IDs but also keeps a log of guests, a proxy can add logging, caching, or security checks before delegating the request to the actual object.
  • Flexibility: You can swap the proxy in and out without changing the client code—just as a club might change its bouncer without affecting the overall experience.

How It Works (In Plain English)

  1. The Real Deal: You have a complex object (think of it as the VIP lounge) that you want to protect from direct access.
  2. The Bouncer (Proxy): The proxy stands at the door. When a request comes in, it checks if the request should be allowed through, based on predefined criteria.
  3. Pass or Deny: If the criteria are met, the proxy creates (or reuses) the real object and delegates the request. Otherwise, it denies access.
  4. Extra Services: In addition to controlling access, the proxy can also log activities, cache results, or perform other housekeeping tasks—ensuring everything runs smoothly.

C# Code Example

C#
using System;

// Subject interface defines common operations.
public interface IVIPService
{
    void AccessService(string customerName);
}

// The RealSubject: the actual VIP service.
public class VIPService : IVIPService
{
    public void AccessService(string customerName)
    {
        Console.WriteLine($"Welcome, {customerName}, to the VIP lounge!");
    }
}

// The Proxy: acts as the bouncer at the door.
public class VIPServiceProxy : IVIPService
{
    private VIPService _vipService;
    private bool _hasAccess;

    public VIPServiceProxy(bool hasAccess)
    {
        _hasAccess = hasAccess;
    }

    public void AccessService(string customerName)
    {
        if (!_hasAccess)
        {
            Console.WriteLine($"Access denied for {customerName}. Please check your membership status.");
            return;
        }

        // Lazy initialization: create the VIPService only if access is granted.
        if (_vipService == null)
        {
            _vipService = new VIPService();
        }

        // Log access and delegate the request.
        Console.WriteLine($"Bouncer: {customerName} is allowed in.");
        _vipService.AccessService(customerName);
    }
}

// Client Code
public class Program
{
    public static void Main()
    {
        // Proxy for a non-member should deny access.
        IVIPService proxyForNonMember = new VIPServiceProxy(hasAccess: false);
        proxyForNonMember.AccessService("Alice");

        Console.WriteLine();

        // Proxy for a member should allow access.
        IVIPService proxyForMember = new VIPServiceProxy(hasAccess: true);
        proxyForMember.AccessService("Bob");
    }
}

The Takeaway

The Proxy Pattern is like having a top-notch bouncer at your club. It controls who gets in, adds extra layers of security, and manages resource-intensive tasks behind the scenes. By intercepting requests and deciding whether to delegate them to the real object, the proxy keeps your code secure, efficient, and flexible.

Happy coding—and may your code always have the best bouncer in town!