• 转:java 委托


    委托模式是软件设计模式中的一项基本技巧。在委托模式中,有两个对象参与处理同一个请求,接受请求的对象将请求委托给另一个对象来处理。委托模式是一项基本技巧,许多其他的模式,如状态模式、策略模式、访问者模式 本质上是在更特殊的场合采用了委托模式。委托模式使得我们可以用聚合来替代继承,它还使我们可以模拟mixin。
      “委托”在C#中是一个语言级特性,而在Java语言中没有直接的对应,但是我们可以通过动态代理来实现委托!代码如下:

    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; //原始对象
        protected Object obj_proxy = null; //代理对象
        //--------------------------------------------
    
        public Delegator() {
        }
    
        public Delegator(Object orgin) {
            this.createProxy(orgin);
        }
       
    
        protected Object createProxy(Object orgin) {
            obj_orgin = orgin;
            //下面语句中orgin.getClass().getClassLoader()为加载器,orgin.getClass().getInterfaces()为接口集
            obj_proxy = Proxy.newProxyInstance(orgin.getClass().getClassLoader(), orgin.getClass().getInterfaces(), this); //委托
            return obj_proxy;
        }
       
    
        protected Object invokeSuper(Method method, Object[] args) throws Throwable {
            return method.invoke(obj_orgin, args);
        }
        //--------------实现InvocationHandler接口,要求覆盖------------
        //下面实现的方法是当委托的类调用toString()方法时,操作其他方法而不是该类默认的toString(),这个类的其他方法则不会。
    
        public Object invoke(Object obj, Method method, Object[] args) throws Throwable {
            // 缺省实现:委托给obj_orgin完成对应的操作
            if (method.getName().equals("toString")) { //对其做额外处理
                return this.invokeSuper(method, args) + "$Proxy";
            } else { //注意,调用原始对象的方法,而不是代理的(obj==obj_proxy)
                return this.invokeSuper(method, args);
            }
        }
    }



    下面的代码,则是作为一个委托的例子,实现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; //原始对象
        private Map proxyClass = null; //代理对象
    
        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")) { //修改size处理逻辑
                Object res2 = new Integer(-1);
                System.out.println("调用委托的方法");
                return res2;
            } else {
                System.out.println("调用原始的方法");
                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();
        }
    }
  • 相关阅读:
    表单重复提交问题
    Win8.1卸载64位Oracle Database 11g的详细图文步骤记录
    A1084. Broken Keyboard (20)
    A1088. Rational Arithmetic (20)
    A1089. Insert or Merge (25)
    A1034. Head of a Gang (30)
    A1013. Battle Over Cities (25)
    A1030. Travel Plan (30)
    A1003. Emergency (25)
    A1076. Forwards on Weibo (30)
  • 原文地址:https://www.cnblogs.com/jearay/p/3673285.html
Copyright © 2020-2023  润新知