Sunday, 24 November 2013

Proxy Design pattern - Implementation[Virtual Proxy]

🚀 Master Core Java & Structural Design Patterns!

Enjoying easy-to-understand design pattern guides? Subscribe to Ram N Java for clean code walkthroughs!

🔔 Subscribe to Ram N Java

Understanding the Virtual Proxy Design Pattern

The Virtual Proxy Pattern is a specialized implementation of the Proxy structural design pattern. It acts as a placeholder for resource-intensive or heavyweight objects, delaying their creation and initialization until they are genuinely needed (a concept known as lazy loading or lazy initialization).

Why Use a Virtual Proxy?

Creating complex objects upfront—such as high-resolution images, large data models, or external service connections—can severely degrade performance and consume heavy memory. A Virtual Proxy solves this problem by:

  • Providing the exact same interface as the real subject.
  • Handling lightweight operations without loading the real object.
  • Instantiating the expensive object only when an essential operation is requested.

Key Components of Virtual Proxy

  1. Subject Interface: Declares common operations for both the real object and the proxy.
  2. Real Subject: The actual heavyweight object that performs the core, memory-intensive work.
  3. Virtual Proxy: Holds a reference to the real subject, controlling its instantiation and access transparently.

Explore More Proxy Pattern Tutorials

Expand your understanding of proxy architectures with these related tutorials from our channel:


Click here to watch in Youtube : https://www.youtube.com/watch?v=ooPPd_LIkzQ

Click the below Image to Enlarge
Virtual Proxy
Virtual Proxy - Class Diagram













































Image.java


interface Image
{
public void displayImage();
}

RealImage.java


//on System A 
class RealImage implements Image
{

private String filename = null;

/**
* Constructor
* 
* @param FILENAME
*/
public RealImage( final String FILENAME )
{
filename = FILENAME; 
loadImageFromDisk();
}

/**
* Loads the image from the disk
*/
private void loadImageFromDisk()
{
System.out.println("   Loading   " + filename);
}

/**
* Displays the image
*/
public void displayImage()
{
System.out.println("   Displaying " + filename+"\n");
}

}

ProxyImage.java


//on System B 
class ProxyImage implements Image
{

private RealImage image    = null;
private String    filename = null;

/**
* Constructor
* 
* @param FILENAME
*/
public ProxyImage( final String FILENAME )
{
filename = FILENAME;
}

/**
* Displays the image
*/
public void displayImage()
{
if( image == null )
{
image = new RealImage(filename);
}
image.displayImage();
}

}


Client.java


class Client
{

/**
* Test method
*/
public static void main( String[] args )
{
final Image IMAGE1 = new ProxyImage("HiResolution_100MB_Dog Photo");
final Image IMAGE2 = new ProxyImage("HiResolution_100MB_Lion photo");

System.out.println("IMAGE1["+IMAGE1+"] calling displayImage first time :");
IMAGE1.displayImage(); // loading necessary
System.out.println("IMAGE1["+IMAGE1+"] calling displayImage second time :");
IMAGE1.displayImage(); // loading unnecessary
System.out.println("IMAGE1["+IMAGE1+"] calling displayImage third time :");
IMAGE1.displayImage(); // loading unnecessary
System.out.println("###############################################################\n");

System.out.println("IMAGE2["+IMAGE2+"] calling displayImage first time :");
IMAGE2.displayImage(); // loading necessary
System.out.println("IMAGE2["+IMAGE2+"] calling displayImage second time :");
IMAGE2.displayImage(); // loading unnecessary

}

}

Output


IMAGE1[ProxyImage@1ad086a] calling displayImage first time :
   Loading   HiResolution_100MB_Dog Photo
   Displaying HiResolution_100MB_Dog Photo

IMAGE1[ProxyImage@1ad086a] calling displayImage second time :
   Displaying HiResolution_100MB_Dog Photo

IMAGE1[ProxyImage@1ad086a] calling displayImage third time :
   Displaying HiResolution_100MB_Dog Photo

###############################################################

IMAGE2[ProxyImage@42719c] calling displayImage first time :
   Loading   HiResolution_100MB_Lion photo
   Displaying HiResolution_100MB_Lion photo

IMAGE2[ProxyImage@42719c] calling displayImage second time :
   Displaying HiResolution_100MB_Lion photo


See also:

  • Proxy Design Pattern - Introduction
  • Proxy Design pattern - Real Time Example[ATM]
  • Proxy Design pattern - Real Time Example [Proxy Server]
  • Proxy Design pattern - Class Diagram
  • Proxy Design pattern - Sequence Diagram
  • Proxy Design pattern - Implementation [Protection Proxy]
  • Proxy Design pattern - Implementation [Remote Proxy]
  • Proxy Design Pattern - Key Points
  • All Design Patterns Links
  • No comments:

    Post a Comment

    Tutorials