Tutorials Design Patterns in C#
Proxy Pattern — Complete Guide
Proxy Pattern — Complete Guide: free step-by-step lesson with examples, common mistakes, and interview tips — part of Design Patterns in C# on Toolliyo Academy.
On this page
Design Patterns in C# · Lesson 12 of 69
Proxy Pattern
GoF Core → Enterprise → Cloud & Craft
GoF Core · 1 — Create & structure · ~6 min · Module 2: Structural Design Patterns
What is this?
Proxy stands in for another object to control access — lazy load, auth checks, caching, or remote calls.
Why should you care?
ShopNest image gallery should lazy-load heavy product media only when opened.
See it live — copy this example
Paste into a C# console or class library project and run dotnet run.
public interface IProductMedia { string LoadUrl(); }
public sealed class RealProductMedia : IProductMedia
{
private readonly string _sku;
public RealProductMedia(string sku) => _sku = sku;
public string LoadUrl()
{
Console.WriteLine("loading from blob...");
return $"https://cdn.shopnest/{_sku}.jpg";
}
}
public sealed class LazyMediaProxy : IProductMedia
{
private readonly string _sku;
private IProductMedia? _real;
public LazyMediaProxy(string sku) => _sku = sku;
public string LoadUrl() => (_real ??= new RealProductMedia(_sku)).LoadUrl();
}
IProductMedia media = new LazyMediaProxy("HD-100");
Console.WriteLine("created");
Console.WriteLine(media.LoadUrl());
Run Example »
Edit the code below and click Run to see the result in Toolliyo’s live editor.
What happened?
- LazyMediaProxy delays RealProductMedia until LoadUrl.
- Same interface as the real subject.
- Virtual/protection proxies follow the same shape.
Practice next
- Run and confirm loading prints only on LoadUrl.
- Call LoadUrl twice; still one real instance.
- Add an auth-checking protection proxy variant.
- Cache the URL string after first load.
- Throw if sku is null in the proxy.
Remember
Surrogate controls access. Same interface as real object. Lazy/protection/remote variants.
ShopNest lazy media proxy
Admin list creates proxies; bytes load on expand.
Outcome: List pages stay light.
Interview prep for this lesson
Practice these questions aloud after reading—each links to a full structured answer.
Sign in to ask a question or upvote helpful answers.
No questions yet — be the first to ask!