In Proxy Pattern, one class represents the functionality of another class. This type of design pattern belongs to structural pattern.
In proxy mode, we create objects with existing objects to provide functional interfaces to the outside world. 意图: Provide a proxy for other objects to control access to this object. 主要解决: Problems caused by direct access to objects, for example, the object to be accessed is on a remote machine. In object-oriented systems, direct access to some objects will bring a lot of trouble to consumers or system structures for some reasons (such as high overhead in object creation, or some operations require security control, or out-of-process access). We can add an access layer to this object when we access this object. 何时使用: Want to do some control when accessing a class. 如何解决: Add the middle layer. 关键代码: The implementation is combined with the delegated class. 应用实例: 1. Shortcuts in Windows. 2. Zhu Bajie went to Gao Cuilan and the result was changed by Sun WuKong. It can be understood like this: abstracting the appearance of Gao Cuilan, both Gao Cuilan and Sun WuKong realized this interface. When Zhu Bajie visited Gao Cuilan, he could not tell that this was Sun WuKong. So Sun WuKong is Gao Cuilan’s agent. 3. You don’t have to buy a train ticket at the railway station, but you can also go to the sales agency. 4. A cheque or bank certificate of deposit is the agent of the funds in the account. Cheques are used in place of cash in market transactions and provide control over the funds on the issuer’s account. 5 、 spring aop . 优点: 1. Clear responsibilities. 2. High expansibility. 3. Intelligence. 缺点: 1. Due to the addition of proxy objects between the client and the real topic, some types of proxy patterns may slow down the processing of requests. 2. The implementation of the agent pattern requires extra work, and the implementation of some agent patterns is very complex. 使用场景: Divided by responsibilities, there are usually the following usage scenarios: 1. Remote agent. 2. Virtual agent. 3. Copy-on-Write agent. 4. Protect or Access agent. 5. Cache agent. 6. Firewall (Firewall) proxy. 7. Synchronization agent. 8. Intelligent reference (Smart Reference) agent 注意事项: 1. The difference between the adapter pattern and the adapter pattern: the adapter pattern mainly changes the interface of the object being considered, while the proxy pattern cannot change the interface of the proxy class. 2. The difference between the decorator mode and the decorator mode: the decorator mode is to enhance the function, while the proxy mode is to control it. We will create a Image Interface and implementation of the Image The entity class of the interface. ProxyImage Is a proxy class that reduces RealImage The memory footprint loaded by the. ProxyPatternDemo Class usage ProxyImage To get the Image Object and display it as required. Create an interface. Create an entity class that implements the interface. When requested, use the ProxyImage To get RealImage The object of the class. Execute the program and output the result: 6.15.1. Introduction ¶
6.15.2. Realize ¶
6.15.3. Step 1 ¶
Image.java ¶
publicinterfaceImage{voiddisplay();}
6.15.4. Step 2 ¶
RealImage.java ¶
publicclassRealImageimplementsImage{privateStringfileName;publicRealImage(StringfileName){this.fileName=fileName;loadFromDisk(fileName);}@Overridepublicvoiddisplay(){System.out.println("Displaying"+fileName);}privatevoidloadFromDisk(StringfileName){System.out.println("Loading"+fileName);}}
ProxyImage.java ¶
publicclassProxyImageimplementsImage{privateRealImagerealImage;privateStringfileName;publicProxyImage(StringfileName){this.fileName=fileName;}@Overridepublicvoiddisplay(){if(realImage==null){realImage=newRealImage(fileName);}realImage.display();}}
6.15.5. Step 3 ¶
ProxyPatternDemo.java ¶
publicclassProxyPatternDemo{publicstaticvoidmain(String[]args){Imageimage=newProxyImage("test_10mb.jpg");//图像将从磁盘加载image.display();System.out.println("");//图像不需要从磁盘加载image.display();}}
6.15.6. Step 4 ¶
Loading test_10mb.jpg
Displaying test_10mb.jpg
Displaying test_10mb.jpg