Java Implementation Agent (proxy) mode

Source: Internet
Author: User

Class diagram


/** * 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!" ");} There are many ways to upgrade and upgrade. It's a kind of money to buy. The task is also a public void upgrade () {System.out.println (this.name +) has risen another level. ");}}

/** * Client is not visible to the Proxied object */public class Gameplayerproxy implements Igameplayer {private Igameplayer Gameplayer = null;//is proxied to Leveling public Gameplayerproxy (String username) {This.gameplayer = new Gameplayer (username) is passed through the constructor function. 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 is not visible to the Proxied object */public class GamePlayerProxy2 implements Igameplayer {private Igameplayer Gameplayer = null;//is proxied to Leveling public GamePlayerProxy2 (String username) {This.gameplayer = new Gameplayer (username) is passed through the constructor function. 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 GamePlay ErProxy3 (Igameplayer gameplayer) {this.gameplayer = Gameplayer; System.out.println ("I am a leveling, I play the role of others.") Be able to transmit 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 directly refer to an object, and the proxy object can act as an intermediary between the client and the target object * Strengths (1). The role of clear and real responsibility is to achieve the actual business logic, not to care about other non-responsibility transactions. After the end of the agent through a complete transaction. The accompanying result is that the programming is concise and clear. (2). The proxy object acts 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 visit (the target Class), one is the proxy object, the real object and the proxy object implement the same interface, first visit the proxy class and then visit the real object to visit. There is still a lot of difference between adorner mode and proxy mode.

The adorner pattern focuses on adding methods dynamically on an object, whereas the proxy mode focuses on controlling access to objects.

In other words, with proxy mode, the proxy class can hide the details of an object from its client. Therefore, when using proxy mode, we often create an instance of an object in a proxy class. And, when we use the adorner mode. Our usual practice is 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 execution 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 finishes its function call */igameplayer ProX y = 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 when invoke in Handler * or: (Face-tangent:) where (connection point), what behavior to perform (notification) * GamePlayerProxy3 is the notification start time when the method name is login, upgrade when the notification end time */gameplayerproxy3 dynamic = new GamePlayerProxy3 (newGameplayer ("Y")); Igameplayer Dynamicplayer = Dynamic.getproxy ();d ynamicplayer.login ("Wangwu", "1234"); Dynamicplayer.killboss ();d ynamicplayer.upgrade ();/* Face Tangent: Some similar business logic needs to be added to many places. They are able to extract it into the tangent plane, which is the transaction plane, such as the log plane, the permission plane, the business aspect */}}

Print:

Start time is: 2014-10-8 17:19:05 login to Zhangsan's role x login successful!

X is playing strange!

X has risen another level. The end time is: 2014-10-8 17:19:05 logon time is: 2014-10-8 17:19:05 role M login for Lisi is 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, can be dynamically passed in the login time is: 2014-10-8 17:19:05 login to 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.