Thilan Dissanayaka Design Patterns Apr 26

Proxy Pattern explained simply

Sometimes you don't want or can't allow direct access to an object. Maybe it's expensive to create, needs special permissions, or you want to control access in some way.
This is where the Proxy Pattern shines!

The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object to control access to it.

What is the Proxy Pattern?

At its core, the Proxy Pattern:

  • Controls access to another object (the "real subject").
  • Can add additional behavior (e.g., lazy initialization, security checks, logging).
  • Makes working with heavy or sensitive objects more efficient and safe.

Think of a security guard at a door. You can't directly enter — you must go through the guard (the proxy).

Real-Life Analogy

Imagine you want to visit a celebrity.
You can't just walk into their home.
You have to talk to their agent (proxy), who controls access, manages schedules, and sometimes answers simple questions on their behalf.

Proxy Pattern in Code (Java)

Let's define an interface for a service:

public interface Image {
    void display();
}

The real object (heavy to load):

public class RealImage implements Image {
    private String filename;

    public RealImage(String filename) {
        this.filename = filename;
        loadFromDisk();
    }

    private void loadFromDisk() {
        System.out.println("Loading " + filename);
    }

    @Override
    public void display() {
        System.out.println("Displaying " + filename);
    }
}

Now the proxy object:

public class ProxyImage implements Image {
    private RealImage realImage;
    private String filename;

    public ProxyImage(String filename) {
        this.filename = filename;
    }

    @Override
    public void display() {
        if (realImage == null) {
            realImage = new RealImage(filename); // Load only when needed
        }
        realImage.display();
    }
}

And use it like this:

public class MainProgram {
    public static void main(String[] args) {
        Image image = new ProxyImage("large_photo.jpg");

        // Image not loaded yet
        System.out.println("Image proxy created.");

        // Now display it
        image.display(); // Loads and displays

        System.out.println();

        // Display again
        image.display(); // Directly displays without loading again
    }
}

Output:

Image proxy created.
Loading large_photo.jpg
Displaying large_photo.jpg

Displaying large_photo.jpg

Key Components

  • Subject Interface (Image): Common interface for RealSubject and Proxy.
  • RealSubject (RealImage): The real object that the proxy represents.
  • Proxy (ProxyImage): Controls access to the RealSubject.

When to Use the Proxy Pattern?

  • When accessing a real object is resource-intensive (e.g., large images, database connections).
  • When you need additional access control (e.g., authentication).
  • When you want to add lazy initialization to heavy objects.
  • When you need remote proxies (access objects over the network).

Types of Proxies

  • Virtual Proxy: Controls access to a resource that is expensive to create.
  • Protection Proxy: Controls access based on permissions.
  • Remote Proxy: Represents an object in a different address space (e.g., server).
  • Smart Proxy: Adds extra functionality like logging or reference counting.

Advantages

✅ Adds control over the real object without changing its code.
✅ Supports lazy initialization and performance optimizations.
✅ Adds extra responsibilities like security or logging.

Disadvantages

❌ Adds complexity to the system.
❌ Can create too many layers of indirection if overused.
❌ Might introduce performance overhead in simple cases.

Real-World Use Cases

  • Database connections: Open only when necessary.
  • File systems: Load files lazily.
  • Security layers: Check permissions before allowing access.
  • Network communication: Stub objects that behave like the real remote object.
  • Caching: Serve cached data instead of querying the original source every time.

Final Thoughts

The Proxy Pattern is all about control — controlling access, controlling initialization, and even controlling additional behavior without touching the real object.

Whenever you need an object to stand in for another, think about using a Proxy!

Happy proxying! 🚪🎯

ALSO READ
SQL injection login bypass
Apr 26 Web App Hacking

SQL Injection (SQLi) is one of the oldest and most fundamental web application vulnerabilities. While it’s becoming rarer in modern web apps due to better coding practices and frameworks,....

ACID Properties in Databases: The Key to Reliable Transactions
Apr 25 Database Systems

When working with databases, one thing is absolutely critical: keeping your data safe, consistent, and reliable. That's where ACID properties come in — a set of principles that ensure every....

Build A Simple Web shell
Mar 23 Web App Hacking

A web shell is a type of code that hackers use to gain control over a web server. It is particularly useful for post-exploitation attacks, and there are various types of web shells available. Some of....

Error based SQL Injection
Apr 26 Web App Hacking

In the previous example, we saw how a classic [SQL Injection Login Bypass](https://hacksland.net/sql-injection-login-bypass) works. SQL Injection is not all about that. The real fun is we can extract....

Observer Pattern explained simply
Apr 26 Design Patterns

When one object needs to notify many other objects about changes in its state **automatically**, the **Observer Pattern** steps in. ## What is the Observer Pattern? - Defines a....

Decorator Pattern explained simply
Apr 26 Design Patterns

When you want to **add new functionalities** to an object **without modifying its structure**, the **Decorator Pattern** comes to the rescue. The Decorator Pattern lets you dynamically **wrap**....