【Java SE】動態代理+Null 物件的實現,javase

來源:互聯網
上載者:User

【Java SE】動態代理+Null 物件的實現,javase

import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.Arrays;import java.util.Collections;import java.util.List;public class NullRobot {    public static void main(String[] args) {        //1        Robot r = newNullRobot();        if(Null.class.isInstance(r)){//        if(r instanceof Null){ //another way            System.out.println("[NullRobot]");        }        System.out.println(r.name());        List<Oper> l = r.ops();        for (Oper oper : l) {            System.out.println(oper.comm());        }                //2        Robot r2 = (Robot) new NullRobotProxyHandler().bind(null);        if(Null.class.isInstance(r2)){            System.out.println("[NullRobot]");        }        System.out.println(r2.name());        List<Oper> l2 = r2.ops();        for (Oper oper : l2) {            System.out.println(oper.comm());        }    }        public static Robot newNullRobot(){        return (Robot) Proxy.newProxyInstance(                NullRobot.class.getClassLoader(), //classLoader可以傳入哪些?滿足什麼條件的classLoader可以被用來載入代理類                new Class[]{Robot.class, Null.class},                 new NullRobotProxyHandler());    }}interface Null{}class NullRobotProxyHandler implements InvocationHandler{//    Robot proxied =     class NRobot implements Robot    , Null //事實上,可以不繼承Null介面,而只要Proxy.newProxyInstance()中的Class[]包含Null.class即可使instanceof判斷為true    {        private String name = "NoneName";        @Override        public String name() {            // TODO Auto-generated method stub            return name;        }        @Override        public List<Oper> ops() {            // TODO Auto-generated method stub            return Collections.emptyList();        }    }        //===another bind way binding real obj with proxy    public Object bind(Object realObj){        realObj = new NRobot();        return (Robot) Proxy.newProxyInstance(                realObj.getClass().getClassLoader(),                 realObj.getClass().getInterfaces(),                 this);    }        @Override    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {        // TODO Auto-generated method stub        return method.invoke(new NRobot(), args);    }    }interface Oper{    String comm();}interface Robot{    String name();    List<Oper> ops();}class SnowRobot implements Robot{    private String name ;    public SnowRobot(String name){        this.name = name;    }    @Override    public String name() {        // TODO Auto-generated method stub        return name;    }    @Override    public List<Oper> ops() {        // TODO Auto-generated method stub        return Arrays.asList(new Oper(){            @Override            public String comm() {                // TODO Auto-generated method stub                return name +" do1";            }        },new Oper(){            @Override            public String comm() {                // TODO Auto-generated method stub                return name+" do2";            }        });    }    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.