1. Proxy: proxy mode 1.1. Overview
L The proxy concept can be understood as: there is an intermediate layer between the starting point and the destination, meaning the proxy. Example: proxy server.
L scenario: There is an image viewing application where each image is large and loading consumes a lot of resources. We hope that a thumbnail is displayed on the interface. You can click the thumbnail, how can I compile this application to load actual images?
L Using proxy is a good method
1.2. Solution: use proxy Mode
Use proxy mode, such as writing code:
// Image Proxy: displays the thumbnail. Class imageproxy {// proxy class Private iimage; Public imageproxy (iimage ){ This. iimage = iimage; } Public void displayimage (){ System. Out. println ("display thumbnail "); } Public void mousepress () {// mouse response, click the thumbnail Iimage. displayimage (); } } Interface iimage { Public abstract void displayimage (); } Class imagedisplay implements iimage {// actual work class Public void displayimage (){ System. Out. println ("show actual image"); // Resource Consumption } } Public class proxy1 { Public static void main (string ARGs []) { Imageproxy proxy = new imageproxy (New imagedisplay (); // It can be assembled. Proxy. displayimage (); // The client is only dealing with the proxy when calling. Proxy. mousepress (); } } |
1.3. Proxy mode Summary
L provides a proxy for other objects to control access to this object. For objects with high overhead, they are created only when they are used, this principle can save us a lot of valuable Java memory.
L examples
L 1. Users of different levels of authorization mechanisms have different access rights to the same object. You can write a proxy class to check user permissions and decide how to access them.
L 2. A client cannot directly operate on an object, but must interact with the object.
L as mentioned in the previous example