SOLID & Design Patterns Interview Question #28
What is the Proxy Pattern?
Structural, Creational & Behavioral Patterns Junior Beginner
Quick Interview Answer
The Proxy Pattern provides a stand-in object that controls access to another object while exposing a compatible interface. The proxy can add behavior such as lazy loading, authorization, remote access, caching, or logging.
Detailed Explanation
The Proxy Pattern provides a stand-in object that controls access to another object while exposing a compatible interface. The proxy can add behavior such as lazy loading, authorization, remote access, caching, or logging.
Unlike an adapter, a proxy normally preserves the same conceptual interface rather than translating one incompatible interface into another.
Code Example
public class AuthorizedReportService : IReportService
{
private readonly IReportService _inner;
private readonly IUserContext _user;
public Task<Report> GetAsync(int id)
{
if (!_user.CanViewReports)
throw new UnauthorizedAccessException();
return _inner.GetAsync(id);
}
}