Proxy mode is my contact with the earlier model, agent is intermediary, intermediary. There are also legal agents, such as lawyers, such as the attorney, the client delegate a part of their rights to the agent, the agent has the agent (principal) part of the authority, and can be the name of the agent to implement these rights, at this time the agent and the client equivalent, Of course, the agent can also in the implementation of the authority to cooperate with their own ability to carry out, of course, can not exceed this permission.
The proxy pattern in Java is similar to the proxy above, and we also create a proxy class for a class (delegate class) to represent it to provide functionality externally.
How do I create a proxy class for a class in Java?
Very simple, we need to create a public interface, the delegate class to implement this interface, and then create an interface implementation class as a proxy class, in the method in this class can directly invoke the same name method in the delegate class, the external class to access, you can use the interface to the proxy class instance, invoke the proxy class method, This indirectly invokes a specific method implementation in the delegate class.
Let's take the example of a legal principal agent to write an example:
Total interface: Ziranren
1 public interface Ziranren {2 void Quanli (); 3}
Principal: Mayun
Public class Implements Ziranren { publicvoid eat () { System.out.println ("Eat Feast Today"); } Public void drink () { System.out.println ("Drinking Atlantic Today"); @Override publicvoid Quanli () { System.out.println (" I confer with my attorney to exercise these rights, at which time the attorney has sole authority to deal with certain matters ");} }
Acting Lawyer: Lvshi
Public class Implements Ziranren { @Override publicvoid Quanli () { new Mayun (). Quanli (); } }
Test class: Clienter
Public class Clienter { publicstaticvoid main (string[] args) { new Lvshi (); Ls. Quanli (); } }
Execution Result:
I confer with my attorney to exercise these rights, at which point the attorney has sole authority to deal with certain matters.
The above is a very simple example, we can see that we want to open up some functions, we can use these functions in the proxy class is referenced, so that we do not want to be exposed to the function, only to open the function we want to open. That is, the delegate class can have a lot of methods, many functions, we can open up, as appropriate, the proxy class is like a door, the delegate class and external callers isolated, only part of the function to give this door, to replace the delegate class to exercise this function, Even if you end up involving yourself (because the corresponding method of the delegate class is ultimately called).
The proxy mode is simple and easy to implement as long as you remember the following key points:
(1) The proxy class implements the same interface as the delegate class
(2) Implement functionality in a delegate class, referencing the same name method of the delegate class in the method of the proxy class
(3) When an external class invokes a method of a delegate class, it directly points to an instance of the proxy class, which is the meaning of the proxy: masking.
Proxy Mode Scenario Description:
(1) When we want to hide a class, we can give it a proxy class
(2) When a class needs different callers to provide different call permissions, you can use the proxy class to implement (the proxy class is not necessarily only one, we can build multiple proxy classes to implement, but also in a proxy-class CICC for the permission to determine the different permissions of the function call)
(3) When we want to extend a function of a class, we can use the proxy mode to do a simple extension in the proxy class (only for simple extensions, before and after the statement referencing the delegate class)
Although the proxy pattern implements strong coupling between the caller and the delegate class, it increases the strong coupling between the proxy class and the delegate class (the method of explicitly invoking the delegate class in the proxy class), and increases the processing time and slows down the processing time after the proxy class is added.
The "proxy mode" of Java design pattern and its application scenario