08 – Incredible Proxy Design Pattern

08 – Incredible Proxy Design Pattern

Proxy Design Pattern in Java: Controlling Access to an Object

The Problem

You need to control access to an object — adding lazy loading, caching, security checks, or remote access — without changing the object itself or the code that uses it. The Proxy pattern provides a surrogate that implements the same interface and controls access to the real object.

Diagram

%%{init: {'theme':'base', 'themeVariables': { 'primaryColor': '#eef2ff', 'primaryTextColor': '#1e293b', 'primaryBorderColor': '#818cf8', 'lineColor': '#94a3b8', 'secondaryColor': '#f0fdf4', 'secondaryBorderColor': '#86efac', 'tertiaryColor': '#fff7ed', 'tertiaryBorderColor': '#fdba74', 'background': '#ffffff', 'mainBkg': '#eef2ff', 'secondBkg': '#f0fdf4', 'tertiaryBkg': '#fff7ed', 'noteBkgColor': '#fff7ed', 'noteBorderColor': '#fdba74', 'actorBkg': '#eef2ff', 'actorBorder': '#818cf8' }}}%%
classDiagram
    class CustomerRepository {
        <<interface>>
        +findById(String id) Customer
    }
    class RealCustomerRepository {
        +findById(String id) Customer
    }
    class CachingCustomerRepositoryProxy {
        -RealCustomerRepository real
        -Map~String,Customer~ cache
        +findById(String id) Customer
    }

    CustomerRepository <|.. RealCustomerRepository
    CustomerRepository <|.. CachingCustomerRepositoryProxy
    CachingCustomerRepositoryProxy --> RealCustomerRepository




Java Example

public interface CustomerRepository {
    Customer findById(String id);
}

public class RealCustomerRepository implements CustomerRepository {
    public Customer findById(String id) {
        System.out.println("Hitting database for " + id);
        return new Customer(id, "Loaded from DB");
    }
}

public class CachingCustomerRepositoryProxy implements CustomerRepository {

    private final RealCustomerRepository real;
    private final Map<String, Customer> cache = new ConcurrentHashMap<>();

    public CachingCustomerRepositoryProxy(RealCustomerRepository real) {
        this.real = real;
    }

    @Override
    public Customer findById(String id) {
        return cache.computeIfAbsent(id, real::findById);
    }
}

Quarkus Example

Quarkus’s CDI container generates client proxies for you automatically (that’s why beans support features like lazy initialization and interception). You rarely write Proxy by hand — instead you use @Cacheable, which the runtime implements as a proxy:

@ApplicationScoped
public class CustomerService {

    @CacheResult(cacheName = "customer-cache")
    public Customer findById(String id) {
        System.out.println("Hitting database for " + id);
        return customerRepository.findById(id);
    }
}

Behind the scenes, Quarkus wraps CustomerService in a proxy that intercepts calls to findById, checks the cache, and only delegates to the real method on a miss — exactly the Proxy pattern, generated for you.

Key Takeaways

  • Use Proxy for lazy loading, access control, caching, logging, or remote invocation, transparently to the client.
  • Common variants: virtual proxy (lazy init), protection proxy (access control), remote proxy (network calls), caching proxy.
  • In Quarkus, most of this is handled declaratively via CDI interceptors/annotations (@Cacheable, @Transactional, security annotations) — understanding Proxy explains how those annotations work under the hood.