I. Preface
A proxy is an intermediary who accepts requests from the requester instead of the server. We've also heard of proxy servers, which help clients to request resources they want. Why do they use proxy? That's because clients can not access servers directly (firewall shielding), while proxy servers can access servers directly. Clients can access servers directly through proxy servers. The result of the request is handed over to the proxy server. The proxy server reorganizes the content and replaces it with its own identity (IP) and returns the result of the request to the user. The disadvantage of this is that the speed of access is greatly reduced, but it is better than nothing.
This is what our proxy model does. If the proxy can handle something (such as the cache that the user has visited and the server has returned without expiration), it does not need to request the server to return directly to the user, but only the things that the proxy server can not handle will be handed to the server again. Of course, this is only a proxy strategy, in order to speed up access, so the proxy server will replace us to access the server.
2. Code examples
It's very troublesome for me to deal with all the things, especially when initialization is very time-consuming, so using proxy can not stop until I have to go out on my own. Let the proxy complete these tasks. This is the proxy mode.
Printable interface: Proxy homology:
package designMode.proxy; public interface Printable { public abstract void setPrinterName(String name); public abstract String getPirnterName(); public abstract void print(String word); }
Printer class: I (equivalent to a real server)
package designMode.proxy; public class Printer implements Printable{ String name; public Printer(String name) { this.name = name; heavyWork(); System.out.println("Successful generation of printer instances..."); } private void heavyWork(){ System.out.println("Myself:"+name); try { Thread.sleep(5000); }catch (InterruptedException e){ e.printStackTrace(); } } @Override public void setPrinterName(String name) { this.name=name; } @Override public String getPirnterName() { return name; } @Override public void print(String word){ System.out.println("Printer"+name+"print now..."); System.out.println(word); System.out.println("Printing is done!"); } }
ProxyPrinter proxy class:
package designMode.proxy; public class ProxyPrinter implements Printable { String name; Printer printer = null; //Do what an agent can do by himself @Override public void setPrinterName(String name) { if(printer!=null){ printer.setPrinterName(name); } this.name = name; } //Do what an agent can do by himself @Override public String getPirnterName() { return name; } //What agents can't do is left to what they can really do (printers). @Override public void print(String word) { check(); printer.print(word); } private synchronized void check(){ if(printer==null){ printer = new Printer(name); } } }
Class Main:
package designMode.proxy; public class Main { public static void main(String[] args) { Printable proxy = new ProxyPrinter(); proxy.setPrinterName("Jiang Shu Ying"); System.out.println("The name of the agent is:"+proxy.getPirnterName()); System.out.println("==Notify the server when you encounter work that the agent can't handle=="); proxy.print("hello,world!"); System.out.println("======================"); proxy.setPrinterName("Ni Ni"); System.out.println("The name of the agent is:"+proxy.getPirnterName()); proxy.print("hello,my country!"); } }
Starting the server is time-consuming (represented by sleeping for 5 seconds), so using the proxy server can easily handle some transactions (name setting, name acquisition), knowing that the proxy server is powerless (print content), the proxy server will notify the server to process, in this case, because of the use of the proxy server. A delegation mechanism is used to combine printer objects, but printer does not know the agent. It is only started. What does this mean? I (printer) can add a lot of agents to start myself without any changes, which is very conducive to expansion. In fact, the lazy loading mechanism is also used here. When we see that the proxy instance is generated only when we have to use it, can we directly use the lazy loading mechanism in the proxy mode? The answer is that it is not conducive to scalability. Without this idea of divide and conquer, the benefits are great. You can switch freely in main, and you can define the same necessary interfaces. This transparency is very useful and is reflected in many models.
Three, summary
Agent mode is a common mode. It generates instances only when necessary. It is also divided into many kinds. It is classified according to use, such as virtual agent, other remote agents, access control agents, etc. We mainly understand the essence of agent implementation, the meaning of agent, and the use of mobile phones. The proxy mode is similar to the decorator mode in that it holds the reference of the same or parent class (the delegation mechanism) and calls the same method in the function to process the response method of the same class with the same name. However, there are differences between the proxy mode and the decorator mode. The proxy mode is to alleviate the work of the principal, and it must be repeated when necessary. To disturb the agent, the decorator mode is to produce new functions and decorate the original attributes.