Skip to content

Latest commit

 

History

History

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

README.md

Virtual Proxy Pattern - Image Proxy

Pattern Overview

The Virtual Proxy pattern provides a placeholder for another object to control access to it. This implementation demonstrates a Virtual Proxy that stands in for an expensive-to-create object (a remote image) until it's actually needed.

Intent

  • Delay the creation and initialization of an expensive object until it's actually needed
  • Provide a surrogate that has the same interface as the real object
  • Control access to the real object and add functionality like lazy loading

Key Components

1. Icon Interface

Icon.java - Defines the common interface for both the proxy and real subject.

public interface Icon {
    public int getIconWidth();
    public int getIconHeight();
    public void paintIcon(Component c, Graphics g, int x, int y);
}

2. ImageProxy (Virtual Proxy)

ImageProxy.java - The proxy that stands in for the real ImageIcon.

Key Features:

  • Holds a reference to the real ImageIcon object (initially null)
  • Implements the same Icon interface as ImageIcon
  • Loads the image asynchronously in a separate thread to avoid blocking the UI
  • Displays a loading message while the image is being fetched
  • Uses thread synchronization for safe concurrent access

Thread Safety:

  • imageIcon is declared volatile for visibility across threads
  • setImageIcon() is synchronized to ensure atomic updates
  • paintIcon() doesn't need synchronization as it's called by a single thread

3. ImageComponent

ImageComponent.java - A custom JComponent that displays the icon.

  • Centers the icon in the frame
  • Delegates painting to the icon's paintIcon() method

4. ImageProxyTestDrive

ImageProxyTestDrive.java - Demonstrates the pattern in action.

How It Works

  1. Initialization: When ImageProxy is created, it only stores the URL. The actual image is not loaded yet.

  2. First Paint: When paintIcon() is called for the first time:

    • The proxy displays "Loading CD cover, please wait..."
    • Spawns a background thread to load the image from the URL
    • Returns immediately without blocking the UI
  3. Image Loading: The background thread:

    • Creates the real ImageIcon from the URL
    • Calls repaint() on the component to trigger a redraw
  4. Subsequent Paints: Once loaded, the proxy delegates all calls directly to the real ImageIcon.

Benefits

  • Improved Responsiveness: UI remains responsive while images load
  • Resource Management: Expensive operations don't block the main thread
  • Transparent: Client code (ImageComponent) doesn't know it's dealing with a proxy
  • Progressive Loading: Shows feedback while loading

Compilation

From the project root directory:

javac -d out ProxyPattern/VirtualProxyPattern/ImageProxy/*.java

Running

From the project root directory:

java -cp out ProxyPattern.VirtualProxyPattern.ImageProxy.ImageProxyTestDrive

Expected Behavior

  1. A window titled "CD Cover Viewer" opens (800x600)
  2. Initially displays "Loading CD cover, please wait..."
  3. After the image loads from the URL, it displays centered in the window
  4. The image loading happens asynchronously without freezing the UI

Design Pattern Type

Structural Pattern - The Virtual Proxy is a structural pattern that controls access to an object by providing a surrogate with the same interface.

Related Patterns

  • Protection Proxy: Controls access based on permissions
  • Remote Proxy: Represents an object in a different address space
  • Smart Proxy: Adds additional functionality when accessing an object
  • Lazy Initialization: Similar concept of delaying expensive operations