java¶¯Ì¬´úÀíÀà
Java¶¯Ì¬´úÀíÀàλÓÚjava.lang.reflect°üÏ£¬Ò»°ãÖ÷ÒªÉæ¼°µ½ÒÔÏÂÁ½¸öÀࣺ
(1)Interface InvocationHandler£º¸Ã½Ó¿ÚÖнö¶¨ÒåÁËÒ»¸ö·½·¨
§public object invoke(Object obj,Method method, Object[] args)
ÔÚʵ¼ÊʹÓÃʱ£¬µÚÒ»¸ö²ÎÊýobjÒ»°ãÊÇÖ¸´úÀíÀ࣬methodÊDZ»´úÀíµÄ·½·¨£¬ÈçÉÏÀýÖеÄrequest()£¬argsΪ¸Ã·½·¨µÄ²ÎÊýÊý×é¡£ Õâ¸ö³éÏó·½·¨ÔÚ´úÀíÀàÖж¯Ì¬ÊµÏÖ¡£
(2)Proxy£º¸ÃÀ༴Ϊ¶¯Ì¬´úÀíÀ࣬×÷ÓÃÀàËÆÓÚÉÏÀýÖеÄProxySubject£¬ÆäÖÐÖ÷Òª°üº¬ÒÔÏÂÄÚÈÝ
protected Proxy(InvocationHandler h)£º¹¹Ô캯Êý£¬ÓÃÓÚ¸øÄÚ²¿µÄh¸³Öµ¡£
static Class getProxyClass (ClassLoader loader, Class[] interfaces)£º»ñµÃÒ»¸ö´úÀíÀ࣬ÆäÖÐloaderÊÇÀà×°ÔØÆ÷£¬interfacesÊÇÕæʵÀàËùÓµÓеÄÈ«²¿½Ó¿ÚµÄÊý×é¡£
static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler h)£º·µ»Ø´úÀíÀàµÄÒ»¸öʵÀý£¬·µ»ØºóµÄ´úÀíÀà¿ÉÒÔµ±×÷±»´úÀíÀàʹÓÃ(¿ÉʹÓñ»´úÀíÀàµÄÔÚSubject½Ó¿ÚÖÐÉùÃ÷¹ýµÄ·½·¨)
ËùνDynamic ProxyÊÇÕâÑùÒ»ÖÖclass£ºËüÊÇÔÚÔËÐÐʱÉú³ÉµÄclass£¬ÔÚÉú³ÉËüʱÄã±ØÐëÌṩһ×éinterface¸øËü£¬È»ºó¸Ãclass¾ÍÐû³ÆËüʵÏÖÁËÕâЩ interface¡£Ä㵱Ȼ¿ÉÒ԰ѸÃclassµÄʵÀýµ±×÷ÕâЩinterfaceÖеÄÈκÎÒ»¸öÀ´Óᣵ±È»£¬Õâ¸öDynamic ProxyÆäʵ¾ÍÊÇÒ»¸öProxy£¬Ëü²»»áÌæÄã×÷ʵÖÊÐԵŤ×÷£¬ÔÚÉú³ÉËüµÄʵÀýʱÄã±ØÐëÌṩһ¸öhandler£¬ÓÉËü½Ó¹Üʵ¼ÊµÄ¹¤×÷
ÔÚʹÓö¯Ì¬´úÀíÀàʱ£¬ÎÒÃDZØÐëʵÏÖInvocationHandler½Ó¿Ú
²Î¼û³ÌÐò Subject.java
²Î¼û³ÌÐò RealSubject.java
²Î¼û³ÌÐò DynamicSubject.java
²Î¼û³ÌÐò Client.java
package com.langsin.dynamicproxy;
//³éÏó½ÇÉ«(֮ǰÊdzéÏóÀ࣬´Ë´¦Ó¦¸ÄΪ½Ó¿Ú)£º
public interface Subject
{
abstract public void request();
}
package com.langsin.dynamicproxy;
//¾ßÌå½ÇÉ«(¾Ö³¤)
public class RealSubject implements Subject
{
public RealSubject()
{
}
public void request()
{
System.out.println("from real subject.");
}
}
package com.langsin.dynamicproxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
//´úÀí´¦ÀíÆ÷(ÃØÊé)
/**
* ¸Ã´úÀíÀàµÄÄÚ²¿ÊôÐÔΪObjectÀ࣬ʵ¼ÊʹÓÃʱͨ¹ý¸ÃÀàµÄ¹¹Ô캯ÊýDynamicSubject(Object obj)¶ÔÆ
Ïà¹ØÎĵµ£º
תÔØ×Ô£ºhttp://blog.csdn.net/seaward/archive/2008/11/26/3376511.aspx
ÓÃgoogleÕÒµ½¹ýÀÏÍâдµÄÏà¹ØµÄÎÄÕ£¬µ«ÊDz»ÍêÕû¡£¸ù¾Ý×Ô¼ºµÄʵ¼ùÖØÐÂдһ¸ö¡£
¼ÙÉ裺ÎÒÃÇÏÖÓÐÒ»¸öjava project½Ð×öoldWebApp£¬Ò»¸öDynamic Web Project½Ð×ödynaWebApp¡£
1¡¢ÔÚoldWebAppµÄ¸ùĿ¼ÏÂÕÒµ½²¢´ò¿ª.projectÎļþ£¬Ð޸ijÉ
<?xml ver ......
1£®ÈçºÎѧϰ³ÌÐòÉè¼Æ£¿
JAVAÊÇÒ»ÖÖƽ̨£¬Ò²ÊÇÒ»ÖÖ³ÌÐòÉè¼ÆÓïÑÔ£¬ÈçºÎѧºÃ³ÌÐòÉè¼Æ²»½ö½öÊÊÓÃÓÚJAVA£¬¶ÔC++µÈÆäËû³ÌÐòÉè¼ÆÓïÑÔÒ²Ò»Ñù¹ÜÓá£Óбà³Ì¸ßÊÖÈÏΪ£¬ JAVAÒ²ºÃCÒ²ºÃûʲô·Ö±ð£¬ÄÃÀ´¾ÍÓá£ÎªÊ²Ã´ËûÃÇÄÜ´ïµ½Èç´Ë¾³½ç£¿ÎÒÏëÊÇÒòΪ±à³ÌÓïÑÔÖ®¼äÓй²Í¨Ö®´¦£¬Áì»áÁ˱à³ÌµÄ¾«Ë裬×ÔÈ»Äܹ»×öµ½Ò»Í¨°Ùͨ¡£ÈçºÎѧϰ³ÌÐòÉè¼ÆÀ ......
Èç¹ûÄãÈ¥ÃæÊÔµÄʱºò£¬ÃæÊÔ¹Ù¿¼Ä㣬ΪʲôҪÔÚjavaÖеÄmain·½·¨Ç°¼Ópublic static voidÕâЩÐÞÊηû£¬ÄãÖªµÀÈçºÎÈ¥»Ø´ðÂð£¿ÏÂÃæÎÒΪ´ó¼Ò½²½âһϰɣ¡Èç¹ûÓÐʲôȱ©µÄ»°£¡»¶Ó´ó¼ÒµÄÖ¸µã£¡
Ê×ÏÈ£¬ÎªÊ²Ã´ÒªÓÃpublic£º
ÒòΪÔÚÔËÐгÌÐòʱ£¬jvmÒªµ÷ÓÃmain·½·¨£¬µ«ÎÒÃǶ¼Ó¦¸ÃÖªµÀjvm ......
/*
³ÌÐò¹¦ÄÜ£º½«ecoli(1).txt·Ö¸î£¬ÆäÖзָîµÄ·Ö¸î·ûÊÇ>
·Ö¸îµÄÎļþÃûÊÇ>gi|????|,"???"±íʾһ¶ÎÊý×Ö£¬ÓÃÊý×Ö×ö×ÓÎļþÃû
ÎļþÒÔ×Ö·û"!"½áβ
³ÌÐòÔ±:xianglinyan
*/
import java.io.*;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataOutputStream;
impo ......