1. What is proxy mode?
The proxy mode is to provide a proxy for other objects to control access to the object. For example, when we buy train tickets, in some places there are train ticket sales, these outlets can help the railway station to implement the function of ticketing, like this mode is the proxy mode. In Java, proxy mode initiates the role of mediations, which can remove functionality or add additional services.
2. Classification of code patterns
A, remote agent: For different geographic objects to provide local area network representative objects, similar to the client and server-like mode.
B, virtual agent: According to the needs of the resource consumption of a large object delay, when it is really necessary to create. For example, when we browse the Web page, sometimes we need to load a large picture, before the image is loaded successfully, we can use a temporary image to temporarily display.
C, protection agent: is to control the access rights of an object. For example, when we browse the post, when we have not registered, we can only browse the function, when we register to post the function.
D, smart Reference Agent: is to provide additional services to the target object. The equivalent is to add some functions or services to the agent.
3. How the proxy mode is implemented
There are two main ways of implementation: One is static agent, the other is dynamic agent
A, static proxy:
The proxy and the Proxied object are determined before the agent. They all implement the same interface or inherit the same abstract class.
To implement proxy mode through inheritance:
1 Package Com.example.DesignPattern.ProxyPattern; 2 3 Public Interface Moveable {4 Public void move (); 5 }
1 PackageCom.example.DesignPattern.ProxyPattern;2 3 ImportJava.util.Random;4 5 Public classCarImplementsmoveable{6 7 @Override8 Public voidMove () {9 Try {TenSYSTEM.OUT.PRINTLN ("Car driving ..."); OneThread.Sleep (NewRandom (). Nextint (1000)); A}Catch(interruptedexception e) { - e.printstacktrace (); - } the } - -}
1 PackageCom.example.DesignPattern.ProxyPattern;2 3 Public classCar1extendsCar {4 @Override5 Public voidMove () {6 LongStartTime =System.currenttimemillis ();7SYSTEM.OUT.PRINTLN ("Car starts ...");8 Super. Move ();9 LongEndTime =System.currenttimemillis ();TenSYSTEM.OUT.PRINTLN ("Car End Drive". Travel time: "+ (Endtime-starttime) +" milliseconds "); One } A}
To implement the proxy pattern in an aggregated way:
1 PackageCom.example.DesignPattern.ProxyPattern;2 3 4 5 Public classCar2Implementsmoveable{6 PrivateCar MCar =NULL;7 PublicCar2 (car car)8 {9MCar =car;Ten } One @Override A Public voidMove () { - LongStartTime =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN ("Car starts ..."); the Mcar.move (); - LongEndTime =System.currenttimemillis (); -SYSTEM.OUT.PRINTLN ("Car End Drive". Travel time: "+ (Endtime-starttime) +" milliseconds "); - } + -}
B. Using the JDK Dynamic agent:
1 PackageCom.example.DesignPattern.AgentPattern;2 3 ImportJava.lang.reflect.InvocationHandler;4 ImportJava.lang.reflect.Method;5 6 Public classTimehandlerImplementsinvocationhandler{7 PrivateObject target =NULL;8 PublicTimehandler (Object target)9 {Ten This. target =Target; One } A /** - * Proxy: The object being proxied - * Method: Methods of the object being proxied the * args: The object of the proxy method - * - * Return value: The return value of the agent's method - */ + @Override - PublicObject Invoke (Object proxy, Method method, object[] args)throwsThrowable { + Method.invoke (target); A return NULL; at } - -}
1 PackageCom.example.DesignPattern.AgentPattern;2 3 ImportJava.lang.reflect.Proxy;4 5 ImportCom.example.DesignPattern.ProxyPattern.Car;6 Importcom.example.DesignPattern.ProxyPattern.MoveAble;7 8 Public classTest {9 Public Static voidMain (string[] args) {TenCar car =NewCar (); OneTimehandler handler =NewTimehandler (car); AClass<?> CLS =Car.getclass (); - /** - * First parameter: Class loader of the class being proxied the * Second parameter: The interface is implemented by a proxy class - * Third parameter: Invocationhandler object, we need to create a sub-class manually, to implement the function - */ -Moveable m =(moveable) proxy.newproxyinstance (Cls.getclassloader (), Cls.getinterfaces (), handler); + M.move (); - } +}
Implementation steps for the JDK Dynamic agent:
1. Create a class that implements the interface Invocationhandler, which must implement the Invoke method.
2. Create a proxy class and interface
3. Call the static method of proxy and create a proxy class: Newproxyinstance (ClassLoader load, class []intefeaces, Involcationhandler h);
4. Calling methods via Proxy
The proxy mode of java-design mode