Java implements Proxy mode, javaproxy
Class Diagram
/*** Gamer interface ** @ author stone **/public interface IGamePlayer {// log on to the game public void login (String user, String password); // kill the monster, main features of online games: public void killBoss (); // upgrade public void upgrade ();}
/*** Gamer * @ author stone **/public class GamePlayer implements IGamePlayer {private String name = ""; // pass the name public GamePlayer (String _ name) through the constructor) {this. name = _ name;} // killBoss () {System. out. println (this. name + "Strange! ");} // You must log on before entering the game. This is a required condition: public void login (String user, String password) {System. out. println ("role with login name" + user + "+ this. name + "Logon successful! ");} // Upgrade. There are many methods to upgrade. You can buy it with money, and do the task with public void upgrade () {System. out. println (this. name + "upgraded again! ");}}
/*** The client is invisible to the proxy object */public class GamePlayerProxy implements IGamePlayer {private IGamePlayer gamePlayer = null; // The proxy object. // It is passed through the constructor to whom the proxy is required. public GamePlayerProxy (String username) {this. gamePlayer = new GamePlayer (username);} // public void killBoss () {this. gamePlayer. killBoss ();} // logon to public void login (String user, String password) {this. gamePlayer. login (user, password);} // upgrade public void upgrade () {this. gamePlayer. upgrade ();}}
/** The client is invisible to the proxy object */public class GamePlayerProxy2 implements IGamePlayer {private IGamePlayer gamePlayer = null; // The proxy object. // It is passed through the constructor to whom the proxy is required. public GamePlayerProxy2 (String username) {this. gamePlayer = new GamePlayer (username);} // public void killBoss () {this. gamePlayer. killBoss ();} // logon to public void login (String user, String password) {System. out. println ("Logon Time:" + new Date (). toLocaleString (); this. gamePlayer. login (user, password);} // upgrade public void upgrade () {this. gamePlayer. upgrade (); System. out. println ("upgrade time:" + new Date (). toLocaleString ());}}
/** The client is invisible to the proxy object */public class GamePlayerProxy3 {private IGamePlayer gamePlayer; // The public GamePlayerProxy3 (IGamePlayer gamePlayer) object is passed by the constructor) {this. gamePlayer = gamePlayer; System. out. println ("I Am a trainer, I am playing another role, and can be passed in dynamically");} public IGamePlayer getProxy () {return (IGamePlayer) Proxy. newProxyInstance (this. getClass (). getClassLoader (), new Class [] {IGamePlayer. class}, new MyInvocationHandler ();} private class MyInvocationHandler implements InvocationHandler {@ Overridepublic Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if (method. getName (). equals ("login") {System. out. println ("Logon Time:" + new Date (). toLocaleString ();} if (method. getName (). equals ("upgrade") {System. out. println ("upgrade time:" + new Date (). toLocaleString ();} method. invoke (gamePlayer, args); return null ;}}}
/** Proxy mode: provides a proxy for other objects to control access to this object. * In some cases, an object is not suitable or cannot directly reference another object. A proxy object can play a mediation role between the client and the target object * advantage (1 ). clear responsibilities the real role is to implement the actual business logic, do not care about other transactions that are not in this responsibility, complete a transaction through a later agent, with the result that the programming is concise and clear. (2) The proxy object can play a mediation role between the client and the target object, which plays a role and protects the target object. (3 ). the high-scalability mode structure is the real object you want to access (target class), the proxy object, and the real object and proxy object implement the same interface, access the proxy class before accessing the objects to be accessed. There are still many differences between the decorator mode and the proxy mode. The modifier mode focuses on the dynamic addition of an object, but the proxy mode focuses on controlling access to the object. In other words, in proxy mode, proxy class can hide the specific information of an object to its customers. Therefore, when using the proxy mode, we often create an object instance in a proxy class. In addition, when we use the decorator pattern, we usually pass the original object as a parameter to the constructor of the decorator. We can use another sentence to summarize these differences: using the proxy mode, the relationship between the proxy and the real object is usually determined at compilation, the modifier can be recursively constructed at runtime. */Public class Test {public static void main (String [] args) {/** common static Proxy: the client does not know the proxy object, call a proxy object to complete its function */IGamePlayer proxy = new GamePlayerProxy ("X"); System. out. println ("Start Time:" + new Date (). toLocaleString (); proxy. login ("zhangSan", "abcd"); proxy. killBoss (); proxy. upgrade (); System. out. println ("End Time:" + new Date (). toLocaleString (); System. out. println ();/** the proxy object enhances the proxy object function */IGamePlayer proxy2 = new GamePlayerProxy2 ("M"); proxy2.login ("lisi", "efg "); proxy2.killBoss (); proxy2.upgrade (); System. out. println ();/** dynamic Proxy: Use the InvocationHandler provided by jdk to call the method of the proxy object * in combination with java. reflect. proxy generates Proxy objects * dynamically passed in by the Proxy object to construct the InvocationHandler. When invoke is in the handler, the function of the method of the Proxy object can be enhanced * or: (face-oriented :) Where (connection point ), action (notification) * GamePlayerProxy3 indicates the notification start time when the method is login, and the notification end time when upgrade is used */GamePlayerProxy3 dynamic = new GamePlayerProxy3 (new GamePlayer ("Y ")); IGamePlayer dynamicPlayer = dynamic. getProxy (); dynamicPlayer. login ("wangwu", "1234"); dynamicPlayer. killBoss (); dynamicPlayer. upgrade ();/** aspect-oriented: some similar business logic needs to be added to many places, so that we can extract it to the aspect, that is, the transaction aspect: such as log aspect, permission aspect, and service aspect */}}
Print:
Start Time: 17:19:05 login name is zhangSan role X login successful! X is playing! X has been upgraded again! End Time: 17:19:05 Logon Time: 17:19:05 Logon Time: Role M with the login name lisi successfully logged on! M is playing! M is upgraded again! Upgrade time: 17:19:05 I am a trainer, and I am playing another role. I can dynamically transfer in the logon time: 17:19:05 login name is wangwu role Y login successful! Y is playing! Upgrade time: 05Y again!
Java: Does the proxy mode implement the InvocationHandler class as the proxy class or $ Proxy0 class as the proxy class?
$ Proxy0 is a proxy class generated by JDK.
Java proxy Mode
This is not like the real proxy mode, right?
The image is simulated.
Net is the Proxy object and calls net. when browse is used, the net check method is called to perform some inspection and other work, and then the browse method of the delegate object is called for real business processing. In fact, the method specified by the delegate object is always executed.
The Proxy class is mainly for the delegate class to pre-process messages, filter messages, and transfer messages to the delegate class and post-processing. The Proxy class does not really implement the service
The Proxy class and InvocationHandler interface provided by Java provide dynamic Proxy class generation
For details, you can refer to the classes and interfaces under java. lang. reflect. In fact, the reflection mechanism of java is mainly used, which is very simple.
The Real class implements the Network interface. Although the Proxy constructor parameter type is Network, classes that implement an interface in java can be directly converted to the interface type.
Network real = new Real ();
Therefore, you can directly pass an instantiated object of the Real class.
This. network. browse (); is actually calling the browse method of the Real class's instantiated object.