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.
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.
This directory demonstrates two common types of proxy patterns:
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 ImageIconIcon- Common interface for proxy and real subjectImageComponent- Component that displays the iconImageProxyTestDrive- Demo application
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 interfacesGumballMachineRemote/- Real-world example of remote monitoring system
- 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)
- Separation of Concerns: Separates the client from the real subject
- Performance Optimization: Can delay expensive operations or cache results
- Transparency: Clients don't know they're using a proxy
- Location Independence: Remote proxies hide network details
- Additional Functionality: Can add features like logging, caching, or access control
Structural Pattern - The Proxy Pattern is a structural pattern that controls access to an object by providing a surrogate with the same interface.
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/*.javajava -cp out ProxyPattern.VirtualProxyPattern.ImageProxy.ImageProxyTestDriveSee the specific subdirectories for detailed running instructions as they require RMI registry setup.
- 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
- 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
For detailed implementation guides and explanations: