Proxy mode
Proxy mode is a common Java design pattern, his characteristic is that the proxy class and the delegate class have the same interface, the proxy class is mainly responsible for the delegate class preprocessing messages, filtering messages, forwarding messages to the delegate class, and post-processing messages. There is usually an association between the proxy class and the delegate class, and the object of a proxy class is associated with an object of a delegate class, and the object of the proxy class does not actually implement the service, but instead provides a specific service by invoking the related method of the object of the delegate class.
The proxy class can be divided into two types according to the agent's creation period.
Static proxy: The source code is generated automatically by the programmer or a specific tool, and then compiled. Before the program runs, the. class file for the proxy classes already exists.
Dynamic Agent: When the program is running, it is created dynamically using the reflection mechanism.
First look at the static proxy:
1, Count.java
Java code:
- Package Net.battier.dao;
- /**
- * Define an Account interface
- *
- * @author Administrator
- *
- */
- Public interface Count {
- //View account method
- public void Querycount ();
- //Modify account method
- public void Updatecount ();
- }
2, Countimpl.java
Java code:
- Package Net.battier.dao.impl;
- Import Net.battier.dao.Count;
- /**
- * Delegate Class (Contains business logic)
- *
- * @author Administrator
- *
- */
- Public class Countimpl implements Count {
- @Override
- public void Querycount () {
- System.out.println ("View account Method ...");
- }
- @Override
- public void Updatecount () {
- System.out.println ("Modify Account Method ...");
- }
- }
- , Countproxy.java
- Package Net.battier.dao.impl;
- Import Net.battier.dao.Count;
- /**
- * This is a proxy class (Enhanced Countimpl implementation Class)
- *
- * @author Administrator
- *
- */
- Public class Countproxy implements Count {
- private Countimpl Countimpl;
- /**
- * Override the default constructor
- *
- * @param Countimpl
- */
- Public Countproxy (Countimpl countimpl) {
- This.countimpl = Countimpl;
- }
- @Override
- public void Querycount () {
- System.out.println ("before transaction processing");
- //method of invoking the delegate class;
- Countimpl.querycount ();
- System.out.println ("after transaction processing");
- }
- @Override
- public void Updatecount () {
- System.out.println ("before transaction processing");
- //method of invoking the delegate class;
- Countimpl.updatecount ();
- System.out.println ("after transaction processing");
- }
- }
3, Testcount.java
Java code:
- Package net.battier.test;
- Import Net.battier.dao.impl.CountImpl;
- Import Net.battier.dao.impl.CountProxy;
- /**
- * Test Count class
- *
- * @author Administrator
- *
- */
- Public class Testcount {
- public static void Main (string[] args) {
- Countimpl Countimpl = new Countimpl ();
- Countproxy countproxy = new Countproxy (Countimpl);
- Countproxy.updatecount ();
- Countproxy.querycount ();
- }
- }
Observation code can find that each proxy class can only serve one interface, so that the development of the program will inevitably generate too many agents, and all the agent operation in addition to the method called, the other operations are the same, then it must be duplicated code. The best way to solve this problem is to complete the proxy function through a proxy class, which must be done using dynamic proxy.
Let's take a look at the dynamic proxy:
The JDK dynamic agent contains a class and an interface:
Invocationhandler Interface:
Public interface Invocationhandler {
public object Invoke (Object Proxy,method method,object[] args) throws Throwable;
}
Parameter description:
Object proxy: Refers to the objects being proxied.
Method: Methods to invoke
object[] args: Parameters required for method invocation
The subclass of the Invocationhandler interface can be imagined as a proxy's final operation class, replacing the Proxysubject.
Proxy class:
The proxy class is an action class that specifically completes the proxy, which can be used to dynamically generate an implementation class for one or more interfaces, which provides the following operations:
public static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces,
Invocationhandler h)
Throws IllegalArgumentException
Parameter description:
ClassLoader Loader: Class loader
Class<?>[] Interfaces: Get all the interfaces
Invocationhandler h: Get subclass instance of Invocationhandler interface
Ps: Class Loader
In the proxy class in the Newproxyinstance () method requires an instance of the ClassLoader class, ClassLoader actually corresponds to the class loader, in Java, there are three kinds of loaders;
Booststrap ClassLoader: This loader is written in C + + and is not visible in general development;
Extendsion ClassLoader: Used to load the extension class, generally corresponding to the class in the Jre\lib\ext directory;
Appclassloader: (default) Loads the class specified by Classpath, which is most commonly used as an loader.
Dynamic Agent
Compared with the static proxy class, dynamic proxy class, the bytecode of dynamic proxy class is generated dynamically by the Java reflection mechanism when the program runs, without the programmer writing its source code manually. The dynamic proxy class not only simplifies programming, but also improves the scalability of the software system, because the Java reflection mechanism can generate any type of dynamic proxy class. The proxy class and the Invocationhandler interface in the Java.lang.reflect package provide the ability to generate dynamic proxy classes.
Dynamic Proxy Example:
1, Bookfacade.java
Java code
- Package Net.battier.dao;
- Public interface Bookfacade {
- public void Addbook ();
- }
2, Bookfacadeimpl.java
Java code
- Package Net.battier.dao.impl;
- Import Net.battier.dao.BookFacade;
- Public class Bookfacadeimpl implements Bookfacade {
- @Override
- public void Addbook () {
- System.out.println ("Add Book Method ... ");
- }
- }
- , Bookfacadeproxy.java
- Package net.battier.proxy;
- Import Java.lang.reflect.InvocationHandler;
- Import Java.lang.reflect.Method;
- Import Java.lang.reflect.Proxy;
- /**
- * JDK Dynamic Proxy proxy class
- *
- * @author Student
- *
- */
- Public class Bookfacadeproxy implements Invocationhandler {
- private Object target;
- /**
- * Binds the delegate object and returns a proxy class
- * @param target
- * @return
- */
- Public object bind (object target) {
- this.target = target;
- //Get proxy object
- return Proxy.newproxyinstance (Target.getclass (). getClassLoader (),
- Target.getclass (). Getinterfaces (), this ); //To bind the interface (this is a flaw, cglib compensates for this flaw)
- }
- @Override
- /**
- * Call method
- */
- Public object Invoke (Object proxy, Method method, object[] args)
- throws Throwable {
- Object result=null;
- System.out.println ("things start");
- //Execution Method
- Result=method.invoke (target, args);
- System.out.println ("End of Thing");
- return result;
- }
- }
3, Testproxy.java
Java code
- Package net.battier.test;
- Import Net.battier.dao.BookFacade;
- Import Net.battier.dao.impl.BookFacadeImpl;
- Import Net.battier.proxy.BookFacadeProxy;
- Public class Testproxy {
- public static void Main (string[] args) {
- Bookfacadeproxy proxy = new Bookfacadeproxy ();
- Bookfacade bookproxy = (bookfacade) proxy.bind (new Bookfacadeimpl ());
- Bookproxy.addbook ();
- }
- }
However, the dynamic proxy of the JDK relies on the interface, and if some classes do not implement the interface, then the JDK proxy cannot be used, which will use the Cglib dynamic proxy.
Dynamic Agent for Java