Http://www.cnblogs.com/soojoe/archive/2012/04/12/2532304.html
The delegation mode is a basic technique in software design pattern. In delegate mode, there are two objects involved in processing the same request, and the object that accepts the request delegates the request to another object for processing. The delegate mode is a basic technique, and many other patterns, such as state mode, policy mode, and visitor pattern, are in essence a delegate mode in more special situations. The delegate mode allows us to use aggregations instead of inheritance, which also allows us to simulate mixin.
"Delegate" is a language-level feature in C #, and there is no direct correspondence in the Java language, but we can implement a delegate through a dynamic proxy! The code is as follows:
Import Java.lang.reflect.InvocationHandler;
Import Java.lang.reflect.Method;
Import Java.lang.reflect.Proxy;
Public abstract class Delegator implements Invocationhandler {
//--------------------------------------------
protected Object obj_orgin = null; Original Object
protected Object obj_proxy = null; Proxy Object
//--------------------------------------------
Public Delegator () {
}
Public delegator (Object orgin) {
This.createproxy (Orgin);
}
Protected object Createproxy (object orgin) {
Obj_orgin = Orgin;
The following statement Orgin.getclass (). getClassLoader () is the loader, Orgin.getclass (). Getinterfaces () is an interface set
Obj_proxy = Proxy.newproxyinstance (Orgin.getclass (). getClassLoader (), Orgin.getclass (). Getinterfaces (), this); Commissioned
return obj_proxy;
}
Protected Object Invokesuper (method, object[] args) throws Throwable {
Return Method.invoke (Obj_orgin, args);
}
--------------Implement the Invocationhandler interface, which requires overwriting------------
The following method is implemented when the delegate's class calls the ToString () method, and other methods are manipulated instead of the default ToString () of the class, and other methods of the class do not.
public object invoke (Object obj, Method method, object[] args) throws Throwable {
Default implementation: Delegate to Obj_orgin to complete the corresponding operation
if (Method.getname (). Equals ("toString")) {//To do extra processing on it
Return This.invokesuper (method, args) + "$Proxy";
} else {//note, call the method of the original object instead of the proxy (Obj==obj_proxy)
Return This.invokesuper (method, args);
}
}
}
The following code, as an example of a delegate, implements the function of the map.
Import java.io.IOException;
Import Java.lang.reflect.Method;
Import java.util.Hashtable;
Import Java.util.Map;
Import Org.apache.commons.logging.Log;
Import Org.apache.commons.logging.LogFactory;
Import Com.bs2.core.UtilLog;
public class Delegator4map extends Delegator {
private static Log _log = Logfactory.getlog (Delegator4map.class);
Private Map orginclass = null; Original Object
Private Map proxyclass = null; Proxy Object
Public Map Getorgin () {
return orginclass;
}
Public Map GetProxy () {
return proxyclass;
}
Public Delegator4map (Map orgin) {
Super (Orgin);
Orginclass = Orgin;
Proxyclass = (Map) super.obj_proxy;
}
public object invoke (Object obj, Method method, object[] args) throws Throwable {
if (Method.getname (). Equals ("size")) {//Modify size processing logic
Object res2 = new Integer (-1);
System.out.println ("Method of invoking a delegate");
return res2;
} else {
SYSTEM.OUT.PRINTLN ("Call the original Method");
return Super.invoke (obj, method, args);
}
}
public static void Main (string[] args) throws IOException {
Delegator4map RTM = new Delegator4map (new Hashtable ());
Map m = Rtm.getproxy ();
M.size ();
}
}
The delegate for Java