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 CoreEnterpriseCloud & 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.

Code
Result

What happened?

  • LazyMediaProxy delays RealProductMedia until LoadUrl.
  • Same interface as the real subject.
  • Virtual/protection proxies follow the same shape.

Practice next

  1. Run and confirm loading prints only on LoadUrl.
  2. Call LoadUrl twice; still one real instance.
  3. Add an auth-checking protection proxy variant.
  4. Cache the URL string after first load.
  5. 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.

Mid PDF Detailed
Proxy Initialization:?
Short answer: The client interacts with the proxy (ProxyImage), which implements the same interface (IImage) as the real subject (RealImage). Real-world example (ShopNest) Use a GoF pattern in ShopNest only when it remov…
Mid PDF Detailed
Virtual Proxy:?
Short answer: Used to delay the creation or initialization of an expensive object until it is actually needed, like the ProxyImage example above. Real-world example (ShopNest) Use a GoF pattern in ShopNest only when it r…
Mid PDF Detailed
Abstract Factory (LoggerFactory):?
Short answer: The LoggerFactory class defines a factory method CreateLogger that returns an ILogger object. This is a generic interface for creating various logger types without specifying the concrete class directly. Re…
Mid PDF Detailed
Redo Functionality:?
Short answer: You can extend the system by adding redo functionality. After an undo operation, you could store the undone command in a separate stack and allow users to redo the previous undo operation. Real-world exampl…
Mid PDF Detailed
Abstract Expression (IExpression):?
Short answer: The IExpression interface declares an Interpret method, which is the core of the interpreter pattern. This method is used to interpret and evaluate expressions. public interface IExpression Example code { i…
Questions on this lesson 0

Sign in to ask a question or upvote helpful answers.

No questions yet — be the first to ask!

Design Patterns in C#
Course syllabus

Design Patterns in C# Tutorial

Module 1: Creational Design Patterns
Module 2: Structural Design Patterns
Module 3: Behavioral Design Patterns
Module 4: Enterprise Design Patterns
Module 5: Modern Enterprise Patterns
Module 6: Microservices & Cloud Patterns
Module 7: ASP.NET Core Architecture Patterns
Module 8: Interview & System Design
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details