Proxy mode in Java

Source: Internet
Author: User

Proxy mode in Java

/*** 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!



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.