【CODE】动态代理

package pac.testin;



import java.lang.reflect.InvocationHandler;

import java.lang.reflect.Method;

import java.lang.reflect.Proxy;



interface Subject {

    void request();

}



class RealSubject implements Subject {

    public void request() {

        System.out.println("Here RealSubject go!!");

    }

}



class userSide implements InvocationHandler {



    Subject subject = null;



    public userSide(Subject subject) {

        this.subject = subject;

    }



    public Object invoke(Object proxy, Method method, Object[] args)

            throws Throwable {

        System.out.println("Get warm up!!");

        method.invoke(subject, args);

        System.out.println("Just relax!!");

        return null;

    }



    public static void main(String[] args) {

        Subject subject = new RealSubject();

        InvocationHandler handler = new userSide(subject);



        Subject proxy = (Subject) Proxy.newProxyInstance(subject.getClass()

                .getClassLoader(), subject.getClass().getInterfaces(), handler);

        proxy.request();

    }

}

 

你可能感兴趣的:(code)