YouTip LogoYouTip

Proxy Pattern

# Proxy Pattern In the Proxy Pattern, a class represents the functionality of another class. This type of design pattern is a structural pattern. The Proxy Pattern controls access to the original object by introducing a proxy object. The proxy object acts as an intermediary between the client and the target object, responsible for forwarding client requests to the target object, while also performing additional processing before or after forwarding the request. In the Proxy Pattern, we create an object with an existing object to provide a functional interface to the outside world. ## Introduction ### Intent Provide a surrogate or placeholder for another object to control access to it. ### Main Problem Solved * The Proxy Pattern addresses issues that may arise when directly accessing certain objects, such as high object creation cost, the need for security control, or remote access. ### Use Cases * When control or additional processing is needed when accessing an object. ### Implementation Approach * **Add an intermediate layer**: Create a proxy class that acts as an intermediate layer for the real object. * **Composition of Proxy and Real Object**: The proxy class holds a reference to the real object and controls access during invocation. ### Key Code * **Proxy Class**: Implements the same interface as the real object and adds additional control logic. * **Real Object**: The object that actually performs the task. ### Application Examples * **Shortcuts**: Shortcuts in Windows act as proxies for files or programs. * **Role Playing**: Sun Wukong acts as a proxy for Gao Cuilan, and Zhu Bajie cannot distinguish. * **Ticket Agency**: When buying train tickets, the agency acts as a proxy for the train station. * **Checks**: Act as a proxy for bank account funds, controlling access to funds. * **Spring AOP**: Uses the Proxy Pattern to implement Aspect-Oriented Programming. ### Advantages * **Separation of Responsibilities**: The Proxy Pattern separates access control from business logic. * **Scalability**: Additional functionality or control can be flexibly added. * **Intelligence**: Access requests can be handled intelligently, such as lazy loading, caching, etc. ### Disadvantages * **Performance Overhead**: Adding a proxy layer may affect the speed of request processing. * **Implementation Complexity**: Certain types of proxy patterns can be complex to implement. ### Usage Recommendations * Choose the appropriate proxy type based on specific needs, such as remote proxy, virtual proxy, protection proxy, etc. * Ensure the proxy class has the same interface as the real object so that the client can use the proxy transparently. ### Notes * **Difference from Adapter Pattern**: The Adapter Pattern changes the interface, while the Proxy Pattern does not. * **Difference from Decorator Pattern**: The Decorator Pattern is used to enhance functionality, while the Proxy Pattern is used to control access. ### Structure **The main core roles involved are:** * **Abstract Subject:** * Defines a common interface for both the real subject and the proxy subject, so that the proxy subject can be used anywhere the real subject is used. * **Real Subject:** * Implements the abstract subject interface and is the real object represented by the proxy object. The client directly accesses the real subject, but in some cases, it can be accessed indirectly through the proxy subject. * **Proxy:** * Implements the abstract subject interface and holds a reference to the real subject. The proxy subject usually provides some additional functionality on top of the real subject, such as lazy loading, permission control, logging, etc. * **Client:** * Uses the abstract subject interface to operate on the real subject or proxy subject, without needing to know which specific implementation class it is. ## Implementation We will create an _Image_ interface and a concrete class that implements the _Image_ interface. _ProxyImage_ is a proxy class that reduces the memory usage of loading _RealImage_ objects. The _ProxyPatternDemo_ class uses _ProxyImage_ to get the _Image_ object to be loaded and displays it as needed. ![Image 2: UML diagram of the Proxy Pattern](#) ### Step 1 Create an interface. ## Image.java public interface Image{void display(); } ### Step 2 Create concrete classes implementing the interface. ## RealImage.java public class RealImage implements Image{private String fileName; public RealImage(String fileName){this.fileName = fileName; loadFromDisk(fileName); } @Override public void display(){System.out.println("Displaying " + fileName); }private void loadFromDisk(String fileName){System.out.println("Loading " + fileName); }} ## ProxyImage.java public class ProxyImage implements Image{private RealImage realImage; private String fileName; public ProxyImage(String fileName){this.fileName = fileName; } @Override public void display(){if(realImage == null){realImage = new RealImage(fileName); }realImage.display(); }} ### Step 3 When requested, use _ProxyImage_ to get the object of _RealImage_ class. ## ProxyPatternDemo.java public class ProxyPatternDemo{public static void main(String[]args){Image image = new ProxyImage("test_10mb.jpg"); // image will be loaded from disk image.display(); System.out.println(""); // image will not be loaded from disk image.display(); }} ### Step 4 Execute the program, output result: Loading test_10mb.jpg Displaying test_10mb.jpg Displaying test_10mb.jpg
← Mongodb RelationshipsFlyweight Pattern β†’