Design Model (2) Learning-dynamic Agency

Source: Internet
Author: User

Dynamic Proxy:A dynamic proxy is an object that is specified as a proxy only when the agent is running, instead of the proxy owner. The core idea of Spring AOP is the dynamic proxy design model.

The following shows the UML class diagram of dynamic Proxy:

The following is a question: who calls the invocationhandler's invoke method and how is the proxy object generated?

Dynamic Proxy:

Package COM. LP. ecjtu. dynamicproxy; import Java. lang. reflect. invocationhandler; import Java. lang. reflect. method;/*** dynamic proxy class, proxy trainer * @ author administrator **/public class gameplayerhandler implements invocationhandler {// proxy object, that is, the target object private object target; Public gameplayerhandler (Object realobject) {this.tar get = realobject ;} /*** sales logic executed before the method of the target object */Public void beforemethod () {system. out. Println ("************ business logic before ************");} /*** business logic executed after the method of execution of the target object */Public void aftermehod () {system. out. println ("************* business logic after ************");} @ override public object invoke (Object proxy, method, object [] ARGs) throws throwable {beforemethod (); // Method for executing the target object, that is, the method object result = method in the real class after reflection. invoke (target, argS); // to prevent account theft, if you want to send a message to us after logging on to the game, directly modify gameplayerserviceimp? // The method is not a good method. You can modify it in this method // The Method Name of the proxy object is equal to login if (method. getname (). equalsignorecase ("login") {system. out. println ("Someone is logging in with your account. Please check if it is me! ") ;}Aftermehod (); return result ;}}

Target class object interface: (proxy class ):

Package COM. LP. ecjtu. dynamicproxy;/*** interface implemented by the target object, to use JDK to generate a proxy object, you must implement an interface * @ author administrator **/public interface igameplayerservice {// The proxy object will implement all methods under the target object interface // log on to the game public void login (string user, string password); // public void killboss (); // upgrade public void upgrade ();}

 

Target implementation class:

Package COM. LP. ecjtu. dynamicproxy;/***** @ author administrator * Target class, proxy object class */public class gameplayerserviceimp implements igameplayerservice {private string playername; Public gameplayerserviceimp () {} public gameplayerserviceimp (string playername) {This. playername = playername;} @ override public void killboss () {system. out. println ("hitting blame ---------") ;}@ override public void login (string user, string password) {system. out. println ("Land game ----------") ;}@ override public void upgrade () {system. out. println ("abnormal upgrading --------");}}

Game client:

Package COM. LP. ecjtu. dynamicproxy; import Java. lang. reflect. proxy; public class gameclient {/*** @ Param ARGs */public static void main (string [] ARGs) {// instantiated by the proxy object, it can also be said that the instantiation of the target object igameplayerservice RealPlayer = new gameplayerserviceimp ("Trainer Zhang San"); // instantiate a handler gameplayerhandler handler = new gameplayerhandler (RealPlayer ); // obtain the classloader = RealPlayer. getclass (). getclassloa Der (); // proxy object to implement the interface of the target object. You can also use new class [] {igameplayerservice. Class} class <?> [] Interfaces = RealPlayer. getclass (). getinterfaces (); // dynamically generates a proxy based on the target object. igameplayerservice proxy = (igameplayerservice) proxy. newproxyinstance (classloader, interfaces, Handler); // The trainer logs on to the proxy. login ("Zhang San", "123"); // blame proxy. killboss (); // upgrade proxy. upgrade ();}}

 

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.