This blog is mainly read by a Daniel's blog wrote, the Daniel wrote the database connection pool implementation, looked at his code, suddenly feel very cow. So here I also recorded my own demo, although not advocating the repetition of the wheel, but many times we are in the process of repeating the wheel to improve the technology, so here I repeat the next round
This blog is mainly about the Java dynamic agent of a demo
The scenario is as follows:
In the LOL game, there is a phenomenon is leveling problem, the actual game account will be asked to leveling, and this leveling here is the agent, to help actual game account holders to help fight the event of the race
UML diagram I do not draw, at present, the knowledge of this has not been to learn, plan to go to learn in a period of time
The first is an interface:
Public interface Gameservice {abstract public void login (), abstract public void rank (), abstract public void normal ();
Actual Game player class:
1 Public classLolgameImplementsGameservice {2 3 @Override4 Public voidLogin () {5SYSTEM.OUT.PRINTLN ("Login LOL Client");6 }7 8 @Override9 Public voidrank () {TenSYSTEM.OUT.PRINTLN ("Qualifying in LOL game"); One } A - @Override - Public voidNormal () { theSYSTEM.OUT.PRINTLN ("Match plug in lol game"); - } -}
View Code
Now want to please leveling, let leveling help us to play qualifying, here business logic is if it is ranked, let leveling dozen, ordinary match plug on their own fight
public static void Main (string[] args) {lolgame lol = new Lolgame ();//actual player//generate leveling, qualifying let leveling play, Match Game to play Gameservice service = (Ga Meservice) Proxy.newproxyinstance (LoLGame.class.getClassLoader (), LoLGame.class.getInterfaces (), New Invocationhandler () {@Overridepublic object Invoke (Object proxy, Method method, object[] args) throws Throwable {if (metho D.getname (). Equals ("Login")) {//The method name is determined, the corresponding business Operation SYSTEM.OUT.PRINTLN ("Let leveling log in so that the following qualifying he can play!") Leveling is also called the actual player's account login "); Method.invoke (lol, args);} else if (Method.getname () equals ("Rank")) {System.out.println ("Now is qualifying, let leveling go and play, don't call the actual game of the qualifying method, so you can be the strongest King");} else if (Method.getname (). Equals ("normal")) {System.out.println ("Now it's a match, with sister paper. "); Method.invoke (lol, args);} return null;}}); Service.login (); Service.rank (); Service.normal ();}
Operating effect:
Let leveling log in so that he can play the next qualifying game! Leveling is also called by the actual player's account login
Login LOL Client
Now is qualifying, let leveling go to play, do not call the actual game of the qualifying method, so you can be the strongest king
Now is the match, with sister paper loaded.
Match plug in LOL game
The code was rough, and the collection that Daniel had mentioned was guaranteed to return himself to LinkedList, which is the difficulty of programming here .
Java Agent's Trivia