Intent: Provide a proxy for other objects to control access to this object
Structure
Or
. Static Proxy
To give a simple example, first define a Ihello interface
Code
Ihello.java
Public interface Ihello {
public void Hello (String name);
}
Then let the implementation of the business logic of the Hellospeaker class implementation Ihello interface, Hellospeaker.java code as follows
Hellospeaker.java
public class Hellospeaker implements ihello{
public void Hello (String name) {
System.out.println ("Hello," +name);
}
}
As you can see, the code in the Hellospeaker class that does not have any logs inserted into it, the implementation of the log service is put into the proxy class, and the proxy class implements the Ihello interface as well:
Helloproxy.java
Import java.util.logging.*;
public class Helloproxy implements ihello{
Private Logger Logger=logger.getlogger (This.getclass (). GetName ());
Private Ihello Helloobject;
Public Helloproxy (Ihello helloobject) {
This.helloobject=helloobject;
}
public void Hello (String name) {
Log ("Hello method starts ..."); Log service
Helloobject.hello (name); Execute business logic
Log ("Hello method ends ..."); Log service
}
private void log (String msg) {
Logger.log (LEVEL.INFO,MSG);
}
}
In the Hello () method of the Helloproxy class, the recording service can be scheduled before and after the business logic is actually implemented, and a test program can actually be written to see how the proxy class is used.
Proxydemo.java
public class Proxydemo {
public static void Main (string[] args) {
Ihello proxy=new Helloproxy (New Hellospeaker ());
Proxy.hello ("Justin");
}
}
Operation Result:
Application:
1. Remote Agent
2. Virtual Agent
3. Protection Agent
4. Only reference
Supplemental Dynamic Agent
A dynamic proxy is a proxy object that creates the desired business object, and then unifies the processing of the various requirements in this proxy object.
1, write a class to implement the corresponding Invocationhandler interface
2. Create an object to Proxy
Dynamic agents can extract some crosscutting issues (log management, permission control) into a module, and then cut into the original normal code as needed when running.
Steps:
1. Create dynamic proxy class
Create a class to implement the Invocationhandler interface. Creating objects in the future is done entirely through this proxy class.
2. Create the appropriate object to proxy in the proxy class
3. Create a static Newinstance method to create the proxy object
4, after the proxy object is created, the proxy object executes any method and the Invoke method is executed before the control information can be added to this method.
5, according to the above steps, log management is extracted into a separate module, at run time to join
6, because the use of newinstance this static method injected object, so the annotation need to use the XML configuration file
7, at this time can be considered for the corresponding method to increase the annotation to enhance the corresponding operation
Agent (proxy)