Skip to content

Proxy

A pattern that places a surrogate object in front of the real object to handle control, lazy loading, or caching.

  • When lazily loading heavy resources.
  • When cache or permission checks are needed before remote calls.
  • Subject
  • Real Subject
  • Proxy

The code below is a simplified Unity example based on the scenario described above.

using System.Collections.Generic;
public interface IRemoteInventoryService
{
IReadOnlyList<string> GetItemIds();
}
public sealed class CachingInventoryProxy : IRemoteInventoryService
{
private readonly IRemoteInventoryService remoteService;
private IReadOnlyList<string> cachedItemIds;
public CachingInventoryProxy(IRemoteInventoryService remoteService)
{
this.remoteService = remoteService;
}
public IReadOnlyList<string> GetItemIds()
{
cachedItemIds ??= remoteService.GetItemIds();
return cachedItemIds;
}
}
  • It clarifies module boundaries and reduces coupling.
  • Features can be extended or integrated without modifying existing code.
  • If wrapper layers become too deep, debugging gets harder.
  • Interfaces should stay small so responsibility boundaries do not blur.

This shows the flow where a proxy handles access control, lazy loading, and caching.

Proxy Flow