Link copied to clipboard!
Proxy
Control access; defer / add behavior around real subject.
C#
// Subject interface
interface IService
{
string Get();
}
// Real (expensive / protected) subject
class RealService : IService
{
public string Get() => "DATA";
}
// Proxy: controls access & adds lazy initialization
class LazyProxy : IService
{
private RealService? _real; // lazily created
public string Get()
{
_real ??= new RealService(); // create on first use
// Extra behaviors could be added here (logging, auth, caching, rate limiting, etc.)
return _real.Get();
}
}
// Usage
IService svc = new LazyProxy();
var first = svc.Get(); // "DATA" (creates RealService)
var second = svc.Get(); // "DATA" (reuses existing RealService)
next