This concept of ambiguity, or because at that time in reading or learning a certain knowledge point, did not be able to turn this knowledge into their own things, stay in the relatively shallow level of understanding of the foundation, not be able to use the above, as those jokes in the same, "These principles of the concubine are aware, can be a bad day." Truth just understand must be bad days, understand, understand, the most important thing is to find ways to apply, the knowledge of the end of the learning into their own knowledge system inside, so that only a real understanding, have a good life possible.
Continue to say the design pattern: The design pattern of the agent, the previous understanding, the comparison of efficiency, that the proxy is the time when the request comes, the proxy object executes first, and then to execute the method of the proxy object. If you follow this generalizing understanding, then any call in the combo class is a proxy pattern.
Or is it the first simple UML:
Both the real object and the proxy object implement this interface, and then, when called, the proxy object is called, and then the object of the proxy is called back to the real object.
//Abstract Roles:Abstract Public classSubject {Abstract Public voidrequest ();}//Real Character: Implement the request () method of subject Public classRealsubjectextendsSubject { PublicRealsubject () {} Public voidrequest () {System.out.println ("From real subject." ); }}//Agent role: Public classProxysubjectextendsSubject {//attributes with a real role as a proxy role PrivateSubject Realsubject; PublicProxysubject (Subject realsubject) { This. Realsubject =Realsubject}//This method encapsulates the request method of the real object Public voidrequest () {prerequest (); Realsubject.request (); //the request method for performing real objects herepostrequest (); } ...}//Client Calls:Realsubject real =NewRealsubject (); Subject Sub=NewProxysubject (real); Sub.request ();
There is also a way to inherit:
The core code is:
// dosomething before Super. Dooperation () //dosomething after
- The invocation of the parent class is used in this way without an interface.
In general, the real object should be to increase access restrictions, for example: not being able to access outside.
When it comes to proxy mode, the JDK implements a dynamic proxy mode, why there is a dynamic proxy, because the above, although the implementation of the proxy, but each class A proxy, the code is bloated, and does not conform to the same function corresponding to a block or a class, or abstract package design concept, So there is a dynamic agent for the JDK, and its UML is:
The specific code:
Packageregularexpression.current;ImportJava.lang.reflect.InvocationHandler;ImportJava.lang.reflect.Method;ImportJava.lang.reflect.Proxy;InterfaceIsubject { Public voidshowname (String name);}classRealsubjectImplementsIsubject {@Override Public voidshowname (String name) {System.out.println (name+ "Shiny Debut"); }}classLoghandlerImplementsInvocationhandler {Object target=NULL; PublicObject Gettarget () {returnTarget; } Public voidsettarget (Object target) { This. target =Target; } @Override Publicobject Invoke (Object proxy, Method method, object[] args)throwsthrowable {Object result=NULL; //logic before calling the target object methodSystem.out.println ("There's a big man coming up below.")); //The method that invokes the target object, which links the proxy to the target classMethod.invoke (target, args); //logic after calling the target object methodSystem.out.println ("Applause Welcome"); returnresult; }} Public classClient {/** * @paramargs*/ Public Static voidMain (string[] args) {Loghandler Loghandler=NewLoghandler (); Loghandler.settarget (Newrealsubject ()); //Create a proxy objectIsubject proxysubject= (Isubject) proxy.newproxyinstance (realsubject.class. getClassLoader (), Realsubject.class. Getinterfaces (), Loghandler); System.out.println ("-------JDK Proxy-------------"); Proxysubject.showname ("Wei-seat"); }}
See this method: Java.lang.reflect.Proxy.newProxyInstance (ClassLoader, class<?>[], Invocationhandler) should be able to be launched:
The dynamic agent of the JDK, the class that must implement the interface can have dynamic proxy, otherwise, the method of the class proxy:
Public Static Object newproxyinstance (ClassLoader loader, Class<?>[] interfaces, Invocationhandler h) throws IllegalArgumentException
Unable to establish an instance of the agent, it cannot be proxied, but cglib can, the specific UML is:
What you can see is that realsubject does not need to implement an interface,
classLoginterceptImplementsmethodinterceptor {Object target=NULL; PublicObject Gettarget () {returnTarget; } Public voidsettarget (Object target) { This. target =Target; } @Override PublicObject Intercept (object arg0, Method arg1, object[] arg2, methodproxy arg3)throwsthrowable {Object result=NULL; //logic before calling the target object methodSystem.out.println ("There's a big man coming up below.")); //The method that invokes the target object, which links the proxy to the target classArg3.invoke (target, arg2); //logic after calling the target object methodSystem.out.println ("Applause Welcome"); returnresult; }}
How to call:
Logintercept logintercept=New logintercept (); Logintercept.settarget (new realsubject ()); Realsubject ProxySubject1= (realsubject) enhancer.create (realsubject. Class, logintercept); System.out.println ("-------cblib-------------"); Proxysubject1.showname ("authority");
This also allows you to understand why spring provides two proxy methods:
The target object has no implementation interface spring will choose to use the Cglib proxy. So, by default, if a target object implements an interface, spring chooses the JDK dynamic agent policy to dynamically create an interface implementation class (Dynamic proxy Class) to proxy the target object, which can be understood in a popular sense that this dynamic proxy class is another version of the target object. So it throws a J ava.lang.ClassCastException between the two when cast. And so, by default, if the target object does not implement any interfaces, spring chooses the Cglib proxy, which generates a dynamic proxy object that is a subclass of the target class.
Proxy mode This is a stage in which it is possible to dress up and clarify a point.
Vague concept (2)