Java Implementation Agent (proxy) mode

Source: Internet
Author: User

/** * Gamer Interface * @author Stone *  */public interface Igameplayer {//Login game public void login (string user, string password); /Kill Monsters, the main features of the online game public void Killboss ();//Upgrade public void Upgrade ();}
/** * player * @author stone * */public class Gameplayer implements Igameplayer {private String name = "";//Pass the name public by constructor Gameplayer (String _name) {this.name = _name;} Hit the blame, the most expected is to kill the old strange public void Killboss () {System.out.println (THIS.name + "in the play Strange! ");} Before you enter the game you must log in, it is a necessary condition public void login (string user, string password) {System.out.println ("login name + user +" role "+ This . Name + "Login successful!" ");} Upgrade, upgrade There are many ways to spend money to buy is a kind of, do the task is a public void upgrade () {System.out.println (this.name +) has risen another level! ");}}

/** * Client is not visible to proxied object */public class Gameplayerproxy implements Igameplayer {private Igameplayer Gameplayer = null;//proxy object// Pass through the constructor to whom you want to leveling public gameplayerproxy (String username) {This.gameplayer = new Gameplayer (username);} Leveling kill public void Killboss () {This.gamePlayer.killBoss ();} Leveling login public void login (string user, string password) {this.gamePlayer.login (user, password);} Leveling upgrade public void Upgrade () {This.gamePlayer.upgrade ();}}

/* * Client not visible to proxied object */public class GamePlayerProxy2 implements Igameplayer {private Igameplayer Gameplayer = null;//proxied object// Pass through the constructor to whom you want to leveling public GamePlayerProxy2 (String username) {This.gameplayer = new Gameplayer (username);} Leveling kill public void Killboss () {This.gamePlayer.killBoss ();} Leveling login public void login (string user, string password) {System.out.println ("Logon time is:" + new Date (). toLocaleString ()); This.gamePlayer.login (user, password);} Leveling upgrade public void Upgrade () {This.gamePlayer.upgrade (); SYSTEM.OUT.PRINTLN ("Upgrade time is:" + new Date (). toLocaleString ());}}

/* * Client is not visible to the Proxied object */public class GamePlayerProxy3 {Private Igameplayer gameplayer;//passed the leveling (proxy) object through the constructor public Gameplayerp Roxy3 (Igameplayer gameplayer) {this.gameplayer = Gameplayer; System.out.println ("I am a leveling, I play the role of others, can be transmitted in a dynamic");} 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 is:" + new Date () . toLocaleString ());} if (Method.getname (). Equals ("Upgrade")) {System.out.println ("Upgrade time is:" + 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 does not fit or cannot refer directly to another object, whereas a proxy object can act as an intermediary between the client and the target object * Advantages (1). The role of clear and real responsibility is to achieve the actual business logic, do not care about other non-responsibility affairs, Complete a transaction through a later agent, with the result that the programming is concise and clear. (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 extensibility mode structure One is really the object you want to access (the target class), one is the proxy object, the real object and the proxy object implement the same interface, first access the proxy class and then access the object that is really to be accessed. There are still a lot of differences between adorner mode and proxy mode. The adorner pattern focuses on dynamically adding methods on an object, whereas the proxy mode focuses on controlling access to objects. In other words, with proxy mode, the proxy class can hide specific information about an object from its clients. Therefore, when using proxy mode, we often create an instance of an object in a proxy class. Also, when we use adorner mode, it is common practice to pass the original object as a parameter to the decorator's constructor.     We can summarize these differences in another sentence: Using proxy mode, the relationship between proxies and real objects is usually determined at compile time, and decorators can be constructed recursively at run time.  */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 by calling */igameplayer Proxy = New Gameplayerproxy ("X"); System.out.println ("Start time is:" + new Date (). toLocaleString ());p roxy.login ("Zhangsan", "ABCD");p Roxy.killboss (); Proxy.upgrade (); System.out.println ("End time is:" + new Date (). toLocaleString ()); System.out.println ();/* * Proxy object enhances the function of the proxied object */igameplayer Proxy2 = new GamePlayerProxy2 ("M");p Roxy2.login ("lISI "," EFG ");p Roxy2.killboss ();p roxy2.upgrade (); System.out.println ();/* Dynamic proxy: Using Invocationhandler provided by the JDK, reflection calls the method of the Proxied object * combined with Java.reflect.Proxy to generate the proxy object * Dynamically passing in the Proxied object constructs Invocationhandler, which can enhance the function of the method of the Proxied object in the invoke of the handler * or: (Face-tangent:) where (connection point), what behavior (notification) * GamePlayerProxy3 is the notification start time when the method name is login, upgrade when the notification end time */gameplayerproxy3 dynamic = new GamePlayerProxy3 (New Gameplayer (" Y ")); Igameplayer Dynamicplayer = Dynamic.getproxy ();d ynamicplayer.login (" Wangwu "," 1234 ");d Ynamicplayer.killboss () ;d Ynamicplayer.upgrade ();/* Face Tangent: Some similar business logic needs to be added to a number of places where it can be extracted into a slice, which is a transactional aspect: such as a log plane, a permission plane, a business section */}}

Print:

The start time is: 2014-10-8 17:19:05 Login to Zhangsan role x login Successful! X is playing strange! X has risen another level! The end time is: 2014-10-8 17:19:05 login time is: 2014-10-8 17:19:05 login to Lisi's role m login successful! M is playing strange! M has risen another level! Upgrade time is: 2014-10-8 17:19:05 I am a leveling, I play the role of others, you can dynamically pass in the login time is: 2014-10-8 17:19:05 login Wangwu role y login success! Y's playing weird! Upgrade time is: 2014-10-8 17:19:05y another level!



Java Implementation Agent (proxy) mode

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.