concept
Proxy mode: Proxy mode is actually an extra proxy class to perform some operations on the original object. For example, sometimes we need to hire a lawyer when we go to a lawsuit, because lawyers have expertise in law and can operate and express our ideas for us. This is what agency means. The proxy mode is divided into two categories: 1. Static proxy (not using the methods in jdk); 2. Dynamic proxy (using the InvocationHandler and Proxy in jdk).
Static proxy is created by programmers or tools to generate source code of proxy class, and then compile the proxy class. The so-called static means that the bytecode file of the proxy class already exists before the program runs, and the relationship between the proxy class and the delegate class is determined before running.
The source code of the dynamic proxy class is dynamically generated by the JVM according to reflection and other mechanisms during the program operation, so there is no bytecode file of the proxy class. The relationship between the proxy class and the delegate class is determined when the program is running.
Example
Here we give an example of a static proxy:
Class diagram:
/** * Gamer interface* */ public interface IGamePlayer { // Log in to the game public void login(String user, String password); // Kill monsters, the main feature of online games public void killBoss(); // Upgrade public void upgrade(); } /** * Gamer* */ public class GamePlayer implements IGamePlayer { private String name = ""; // Pass the name through the constructor public GamePlayer(String _name) { this.name = _name; } // When fighting monsters, the most expected is to kill the old monster public void killBoss() { System.out.println(this.name + "Kill monsters!"); } // You must log in before entering the game. This is a necessary condition. public void login(String user, String password) { System.out.println("Login name is" + user + " Role" + this.name + "Login successfully!"); } // Upgrade, there are many ways to upgrade, spending money to buy, and doing tasks is also a kind of public void upgrade() { System.out.println(this.name + "Another level!"); } } /** * The client is not visible to the proxy object*/ public class GamePlayerProxy implements IGamePlayer { private IGamePlayer gamePlayer = null;//Proxy object// Pass through the constructor to whom to practice public GamePlayerProxy(String username) { this.gamePlayer = new GamePlayer(username); } // Recipient to kill monsters public void killBoss() { this.gamePlayer.killBoss(); } // Recipient to login public void login(String user, String password) { this.gamePlayer.login(user, password); } // Practice Upgrade public void upgrade() { this.gamePlayer.upgrade(); } } /* * The client is not visible to the proxy object*/ public class GamePlayerProxy2 implements IGamePlayer { private IGamePlayer gamePlayer = null;//Proxy object// Pass through the constructor to whom to practice public GamePlayerProxy2(String username) { this.gamePlayer = new GamePlayer(username); } // Recipient kill monsters public void killBoss() { this.gamePlayer.killBoss(); } // Recipient login public void login(String user, String password) { System.out.println("LocaleString()"); this.gamePlayer.login(user, password); } // Practice Upgrade() public void upgrade() { this.gamePlayer.upgrade(); System.out.println("Upgrade time is:" + new Date().toLocaleString()); } } /* * The client is not visible to the proxy object*/ public class GamePlayerProxy3 { private IGamePlayer gamePlayer; // Pass the proxy (proxy) object through the constructor public GamePlayerProxy3(IGamePlayer gamePlayer) { this.gamePlayer = gamePlayer; System.out.println("I am a proxy, the character I play is someone else's, and can be dynamically passed in"); } public IGamePlayer getProxy() { return (IGamePlayer) Proxy.newProxyInstance(this.getClass().getClassLoader(), new Class[]{IGamePlayer.class}, new MyInvocationHandler()); } private class MyInvocationHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (method.getName().equals("login")) { System.out.println("LocaleString()"); } if (method.getName().equals("upgrade")) { System.out.println("Upgrade time is: " + new Date().toLocaleString()); } method.invoke(gamePlayer, args); return null; } } } public class Test { public static void main(String[] args) { /* * Normal static proxy: The client does not know the proxy object, and the proxy object completes its function call*/ IGamePlayer proxy = new GamePlayerProxy("X"); System.out.println("start time is: " + new Date().toLocaleString()); proxy.login("zhangSan", "abcd"); proxy.killBoss(); proxy.upgrade(); System.out.println("end time is: " + new Date().toLocaleString()); System.out.println(); /* * The proxy object enhances the function of the proxy object*/ 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 reflect the method of calling the proxy object* Combined with java.reflect.Proxy to generate proxy objects* Dynamically enter the proxy object to construct the InvocationHandler, and the functions of the proxy object can be enhanced when invoke in the handler* Or in other words: (Edit-oriented:) Where (connection point), what behavior is performed (notification) * In GamePlayerProxy3, it is the notification start time when the method is called login, and the notification end time when the upgrade*/ GamePlayerProxy3 dynamic = new GamePlayerProxy3(new GamePlayer("Y")); IGamePlayer dynamicPlayer = dynamic.getProxy(); dynamicPlayer.login("wangwu", "1234"); dynamicPlayer.killBoss(); dynamicPlayer.upgrade(); /* * Edit-oriented: Some similar business logic needs to be added to many places, so we can extract it into the section, which is the transaction section: such as log section, permission section, business section*/ } } Print:
The start time is: 2014-10-8 17:19:05 Login role X with zhangSan is successfully logged in! X is fighting monsters! X has been upgraded to another level! The end time is: 2014-10-8 17:19:05 Login time is: 2014-10-8 17:19:05 Login role M with the name lisi is successfully logged in! M is fighting monsters! M has been upgraded to another level! The upgrade time is: 2014-10-8 17:19:05 I am a proxy player. The character I play is someone else's, and I can dynamically pass it in to log in. The time is: 2014-10-8 17:19:05 The login character Y with the name wangwu was successfully logged in! Y is fighting monsters! The upgrade time is: 2014-10-8 17:19:05 Y has been upgraded to another level!
advantage
(1) The role of clear responsibilities is to realize actual business logic, and you don’t have to care about other non-responsible affairs. You can complete a transaction through a later agent. The accompanying result is concise and clear programming.
(2) The proxy object can act as an intermediary between the client and the target object, which plays a role and protects the target object.
(3) High scalability