Sunday 24 November 2013

Proxy Design pattern - Implementation[Virtual Proxy]


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