Design Patterns Mastery

Proxy Pattern: Interception, Lazy-Loading, and Security

1 Views Updated 5/4/2026

The Proxy Pattern

A Proxy is a "Stand-in" for another object. It has the same interface as the original, but it intercepts all calls to allow for extra logic like **Lazy Loading**, **Security checks**, or **Logging**. It is the foundation of Aspect-Oriented Programming (AOP).

1. Virtual Proxy (Lazy Loading)

Why load a 50MB image from the database if the user might never scroll down to see it? The Proxy returns a "Placeholder" immediately and only performs the heavy database query the second someone calls .Render().

public class ImageProxy : IImage 
{
    private RealImage _realImage;
    private string _filename;

    public void Display() 
    {
        // Only create the heavy object when EXPLICITLY needed!
        if (_realImage == null) _realImage = new RealImage(_filename);
        _realImage.Display();
    }
}

2. Protection Proxy

The Proxy checks if the current user is an 'Admin' before passing the call to the actual DeleteDatabase() method. This keeps your business logic classes clean because they don't have to contain security checks.

4. Interview Mastery

Q: "How does Entity Framework Core use Proxies for Lazy Loading?"

Architect Answer: "When you mark a property as `virtual`, EF Core creates a dynamic class (a Proxy) that inherits from your entity. When you access `user.Posts`, you aren't talking to your class; you are talking to the Proxy. The Proxy detects the property access, fires a SQL query to the database, populates the list, and then returns the data. To the developer, it looks like magic, but it is actually the Proxy pattern intercepting property access."

Design Patterns Mastery
1. Introduction to Design Patterns
Introduction to GoF Patterns: Why patterns matter? SOLID Principles: The foundation of all patterns DRY, KISS, and YAGNI (Architectural Philosophy)
2. Creational Patterns
Singleton Pattern: Thread-safety & Captive Dependencies Factory Method: Abstracting complex object creation Abstract Factory: Creating families of related objects Builder Pattern: Constructing complex fluents Prototype Pattern: Cloning high-cost objects
3. Structural Patterns
Adapter Pattern: Bridging incompatible interfaces Bridge Pattern: Decoupling abstraction from implementation Composite Pattern: Managing tree structures & hierarchies Decorator Pattern: Enhancing behavior without inheritance Facade Pattern: Simplifying complex library subsystems Flyweight Pattern: Drastically reducing memory footprint Proxy Pattern: Interception, Lazy-Loading, and Security
4. Behavioral Patterns
Chain of Responsibility: Middleware & Pipeline Architecture Command Pattern: Implementing Undo/Redo & Queueing Interpreter Pattern: Building domain-specific languages Iterator Pattern: Unified traversal of collections Mediator Pattern: Decoupling components with MediatR Memento Pattern: Capturing and restoring object state Observer Pattern: Pub/Sub & Event-driven architecture State Pattern: Managing complex object lifecycles Strategy Pattern: Swappable algorithms at runtime Template Method: Defining skeleton algorithms Visitor Pattern: Separating operations from data structures
5. Modern Enterprise & Cloud Patterns
Repository & Unit of Work (The EF Core Standard) CQRS Pattern (Command Query Responsibility Segregation) Circuit Breaker & Retry Patterns (Resilience with Polly) Dependency Injection Pattern (The Modern King)