Advanced Java September 20 ,2025

Proxy Design Pattern in Java

Introduction

In software systems, there are scenarios where direct access to an object is undesirable. This could be due to security concerns, expensive object creation, or remote access. The Proxy Pattern provides a substitute or placeholder for another object to control access, delay creation, or add additional functionality.

Think of it like a bodyguard: you don’t meet a celebrity directly—you go through a bodyguard who controls access and protects them.

Definition

The Proxy Pattern is a structural design pattern that provides a surrogate or placeholder for another object. It controls access to the original object, allowing additional actions before or after forwarding requests.

Key Idea: The client interacts with a proxy instead of the real object, and the proxy handles requests, optionally delegating them to the real object.

Types of Proxies

  1. Virtual Proxy: Creates expensive objects only when needed (lazy loading).
  2. Protection Proxy: Controls access based on permissions or roles.
  3. Remote Proxy: Represents an object in a different address space (e.g., remote server).
  4. Smart Proxy: Adds extra functionality, like logging, caching, or reference counting.

When to Use the Proxy Pattern

  • When object creation is resource-intensive and should be delayed.
  • When access control is required for certain users or roles.
  • When you want to hide the complexity of interacting with a remote object.
  • To add additional functionality (logging, caching) without modifying the real object.

Real-World Example

  • ATM card: Acts as a proxy to the bank account. It ensures proper authentication and access control before allowing transactions.
  • Virtual Image Viewer: Loads large images lazily to save memory.

Java Implementation

Let’s implement a Virtual Proxy example for image loading.

Step 1: Define the Subject Interface

// Subject Interface
interface Image {
    void display();
}

Step 2: Create the Real Subject

// Real Subject
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);
    }
}

Step 3: Create the Proxy Class

// Proxy
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); // Lazy loading
        }
        realImage.display();
    }
}

Step 4: Client Usage

public class Main {
    public static void main(String[] args) {
        Image image1 = new ProxyImage("photo1.jpg");
        Image image2 = new ProxyImage("photo2.jpg");

        // Image will be loaded only when display() is called
        image1.display(); 
        image1.display(); // Already loaded, no additional load
        image2.display();
    }
}

Output:

Loading photo1.jpg
Displaying photo1.jpg
Displaying photo1.jpg
Loading photo2.jpg
Displaying photo2.jpg

Observation: The real image object is created only when needed, saving memory and processing.

Advantages of the Proxy Pattern

  1. Lazy Initialization: Creates expensive objects only when required.
  2. Access Control: Protects sensitive objects from unauthorized access.
  3. Logging and Auditing: Can add extra functionality without modifying real objects.
  4. Remote Access: Simplifies interaction with remote objects.

Disadvantages

  1. Extra Layer: Adds an additional layer of abstraction, which may increase complexity.
  2. Overhead: Proxy operations may add slight performance overhead.
  3. Complexity in Implementation: Managing multiple proxy types can make the codebase harder to maintain.

Summary

The Proxy Pattern is a powerful tool to control access, delay object creation, and enhance functionality without changing the real object. By acting as a surrogate, proxies simplify client interaction and improve system performance.

Next, we can continue with Behavioral Design Patterns, which focus on how objects interact and communicate, starting with the Observer Pattern, one of the most commonly used behavioral patterns.

 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Generics P...
Advanced Java August 08 ,2025

Generics Part- 2

Collection...
Advanced Java July 07 ,2025

Collections Framewor...

Mastering...
Advanced Java August 08 ,2025

Mastering Java Multi...

Annotation...
Advanced Java August 08 ,2025

Annotations

Java Memor...
Advanced Java August 08 ,2025

Java Memory Manageme...

Java Lambd...
Advanced Java August 08 ,2025

Java Lambda Expressi...

Java Funct...
Advanced Java August 08 ,2025

Java Functional Inte...

Java Strea...
Advanced Java August 08 ,2025

Java Stream API

JDBC (Java...
Advanced Java August 08 ,2025

JDBC (Java Database...

JDBC (Java...
Advanced Java September 09 ,2025

JDBC (Java Database...

Annotation...
Advanced Java August 08 ,2025

Annotations

Generics
Advanced Java August 08 ,2025

Generics

Java I/O (...
Advanced Java August 08 ,2025

Java I/O (NIO)

Introducti...
Advanced Java September 09 ,2025

Introduction to Desi...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Other Prin...
Advanced Java September 09 ,2025

Other Principles Beh...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Creational...
Advanced Java September 09 ,2025

Creational Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

In Creatio...
Advanced Java September 09 ,2025

In Creational Design...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Builder De...
Advanced Java September 09 ,2025

Builder Design Patte...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Structural...
Advanced Java September 09 ,2025

Structural Design Pa...

Design Pat...
Advanced Java September 09 ,2025

Design Patterns in J...

Chain of R...
Advanced Java September 09 ,2025

Chain of Responsibil...

Command De...
Advanced Java September 09 ,2025

Command Design Patte...

Interprete...
Advanced Java September 09 ,2025

Interpreter Design P...

Iterator D...
Advanced Java September 09 ,2025

Iterator Design Patt...

Mediator D...
Advanced Java September 09 ,2025

Mediator Design Patt...

Memento De...
Advanced Java September 09 ,2025

Memento Design Patte...

Observer D...
Advanced Java September 09 ,2025

Observer Design Patt...

State Desi...
Advanced Java September 09 ,2025

State Design Pattern...

Strategy D...
Advanced Java September 09 ,2025

Strategy Design Patt...

Template M...
Advanced Java September 09 ,2025

Template Method Desi...

Visitor De...
Advanced Java September 09 ,2025

Visitor Design Patte...

Prototype...
Advanced Java September 09 ,2025

Prototype Design Pat...

Java 8+ Fe...
Advanced Java October 10 ,2025

Java 8+ Features

SOLID Prin...
Advanced Java October 10 ,2025

SOLID Principles in...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

Custom Imp...
Advanced Java October 10 ,2025

Custom Implementatio...

How Iterat...
Advanced Java October 10 ,2025

How Iterators Work i...

How Concur...
Advanced Java October 10 ,2025

How ConcurrentHashMa...

Comparable...
Advanced Java October 10 ,2025

Comparable vs Compar...

Get In Touch

G06, Kristal Olivine Bellandur near Bangalore Central Mall, Bangalore Karnataka, 560103

+91-8076082435

techiefreak87@gmail.com