Java Dynamic Agent (Knowledge point Literacy)

Source: Internet
Author: User

Dynamic agents first say the proxy mode, then the dynamic agent

In proxy mode, there will be proxy objects, and the Proxied object (the target business object). The proxy object intercepts access to the target business object. Similar to real teachers and brokers.

/** 老师的接口*/public interface drawer {public void sing(double money);public void draw(double money);public void sleep();}
 /** A specific teacher: Xu Teacher */public class Xulaoshi implements drawer {@Overridepublic void Sing (double money) {System.out . println ("Teacher Xu taught a song!") "); System.out.println ("Xu Teacher earned" + Money + "RMB");} @Overridepublic void Draw (double money) {System.out.println ("Teacher Xu teaches a painting! "); System.out.println ("Xu Teacher earned" + Money + "RMB");} @Overridepublic void Sleep () {System.out.println ("Teacher Xu is too tired ... Rest for a while ");}} /** proxy Class A, agent Xu teacher */public Class A implements drawer {//Hold real business object Private drawer s;//receive real business object public A (drawer s) {THIS.S = s;} @Overridepublic void Sing (double money) {//do an intercept if (Cash < 100000) {System.out.println ("too little to Go"); return;} System.out.println ("A extract" + Money * 0.6 + "commission"); S.sing (Money * 0.4);} @Overridepublic void Draw (double money) {//do an intercept if (Cash < 500000) {System.out.println ("too little, add money!") "); return;} System.out.println ("A extract" + Money * 0.6 + "commission"); S.draw (Money * 0.4);} @Overridepublic void Sleep () {System.out.println ("Help to set a hotel ... "); S.sleep ();}}  
/** 演示:代理模式* 代理:代理对象主要用来拦截目标业务对象(被代理对象)的访问。*  A:代理对象要持有真实业务对象。(通过构造函数接收业务对象,并用成员变量记录)*  B:代理对象要和真实业务对象,具备相同的行为方法(实现相同接口)*  C:代理对象拦截对真实对象的访问,可以修改访问的参数、返回值,甚至拦截访问* 代理模式与装饰模式区别*  A:代理模式侧重与拦截,装饰模式侧重与加强*  B:Java中自带的代理中,只能对接口进行代理。装饰只需要与被装饰类有共同父类。*/public class ProxyDemo01 {public static void main(String[] args) {// 创建被代理对象Xulaoshi Xulaoshi = new Xulaoshi();// 创建代理对象A  a= new A(Xulaoshi);// 访问方法a.sing(500);System.out.println("----------");a.draw(1000000);System.out.println("----------");a.sleep();}}

The following dynamic agent

/** Demo: Dynamic agent * Dynamic Proxy: A proxy object generated during program run * in the process of running the program, dynamically generate a class in memory, the proxy needs to be the target object of the proxy, and generate the object of this proxy class. * How does Java generate a proxy class in a vacuum, and agent Xu teacher? We want to view a api:* static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) * Clas Sloader Loader: Class loader, general class loader with proxied object * class[] interfaces: An array of class objects for the interface of the Proxied object, class<?>[] getinterfaces () * Invoca  Tionhandler h: Call Processor */public class ProxyDemo02 {public static void main (string[] args) {//Create proxied object final Xulaoshi Xulaoshi = new Xulaoshi ();//Prepare the three parameters of the dynamic agent//class loader of the proxy class ClassLoader loader =xulaoshi. GetClass (). getClassLoader ();// An array of class objects for the interface of the Proxied object class<?>[] Interfaces =xulaoshi. GetClass (). Getinterfaces ();//xulaoshi. GetClass (). Getinterfaces (); {drawer.class}//Call processor Invocationhandler h = new Invocationhandler () {//When we invoke the function of the proxy object, the proxy object actually calls the Invoke function in the processor// We need to write the Intercept logic in invoke! public object invoke (object proxy, Method method, object[] args) throws Throwable {//Object proxy: Is the proxy object itself//Method meth OD: Object of the real method currently being called//object[] args : The real argument to the real method that is currently being called string name = Method.getname ();//The name of the method to be used to make different interception logic if ("Sing". Equals (name) | | "Draw". Equals (name)) {Double money = (double) args[0];d ouble min = ' sing '. Equals (name)? 100000:500000;if (Money < min) {System.out.println ("too little money, not to go"); return null;} System.out.println ("A extract" + Money * 0.6 + "commission"); return Method.invoke (Xulaoshi, Money * 0.4);} Return Method.invoke (Xulaoshi, args);}};/  /static Object newproxyinstance (ClassLoader loader, class<?>[] interfaces, Invocationhandler h) Drawer p = (drawer) Proxy.newproxyinstance (loader, interfaces, h);p. Sing;p. Draw (1000000);p. Sleep ();}

Java Dynamic Agent (Knowledge point Literacy)

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.