Skip to content

Latest commit

 

History

History

README.md

Proxy Pattern

The Proxy Pattern provides a surrogate or placeholder for another object to control access to it. This directory contains implementations of different types of proxy patterns.

Pattern Overview

A proxy controls access to the original object, allowing you to perform something either before or after the request gets through to the original object. The proxy has the same interface as the real subject, making it transparent to clients.

Types of Proxies

This directory demonstrates two common types of proxy patterns:

1. Virtual Proxy

Location: VirtualProxyPattern/ImageProxy/

A Virtual Proxy controls access to a resource that is expensive to create. It delays the creation and initialization of an expensive object until it's actually needed.

Use Case: Loading images asynchronously

  • Displays a placeholder while the actual image loads
  • Prevents UI blocking during expensive operations
  • Provides progressive loading feedback

Key Classes:

  • ImageProxy - Proxy that stands in for ImageIcon
  • Icon - Common interface for proxy and real subject
  • ImageComponent - Component that displays the icon
  • ImageProxyTestDrive - Demo application

Read more →

2. Remote Proxy

Location: RemoteProxyPattern/

A Remote Proxy acts as a local representative for an object that lives in a different address space (typically on another JVM or machine). It handles the communication details so the client can interact with the remote object as if it were local.

Use Cases:

  • Java RMI (Remote Method Invocation)
  • Distributed systems communication
  • Client-server architectures

Subdirectories:

  • RemoteInterfaceDemo/ - Basic RMI example demonstrating remote interfaces
  • GumballMachineRemote/ - Real-world example of remote monitoring system

Pattern Intent

  • Control Access: Add a layer of control when accessing objects
  • Lazy Initialization: Delay expensive object creation (Virtual Proxy)
  • Remote Communication: Represent remote objects locally (Remote Proxy)
  • Protection: Add access control and permissions (Protection Proxy)
  • Smart Reference: Add additional functionality like reference counting (Smart Proxy)

Benefits

  1. Separation of Concerns: Separates the client from the real subject
  2. Performance Optimization: Can delay expensive operations or cache results
  3. Transparency: Clients don't know they're using a proxy
  4. Location Independence: Remote proxies hide network details
  5. Additional Functionality: Can add features like logging, caching, or access control

Design Pattern Classification

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

Compilation

To compile all proxy pattern examples from the project root directory:

# Compile Virtual Proxy
javac -d out ProxyPattern/VirtualProxyPattern/ImageProxy/*.java

# Compile Protection Proxy
javac -d out ProxyPattern/ProtectionProxy/Matchmaking/*.java

# Compile Remote Proxy examples
javac -d out ProxyPattern/RemoteProxyPattern/RemoteInterfaceDemo/*.java
javac -d out ProxyPattern/RemoteProxyPattern/GumballMachineRemote/*.java

Running Examples

Virtual Proxy (Image Loading)

java -cp out ProxyPattern.VirtualProxyPattern.ImageProxy.ImageProxyTestDrive

Remote Proxy Examples

See the specific subdirectories for detailed running instructions as they require RMI registry setup.

Other Proxy Types (Not Implemented Here)

  • Protection Proxy: Controls access based on permissions or access rights
  • Smart Proxy: Adds additional functionality like reference counting or locking
  • Caching Proxy: Caches results to avoid expensive operations
  • Firewall Proxy: Controls access to network resources
  • Synchronization Proxy: Provides thread-safe access to an object

Related Patterns

  • Adapter Pattern: Adapter changes the interface, while Proxy keeps the same interface
  • Decorator Pattern: Decorator adds responsibilities, while Proxy controls access
  • Facade Pattern: Facade provides a simplified interface, while Proxy provides the same interface

Learn More

For detailed implementation guides and explanations: