代码源自 尚硅谷 java基础 day19_10反射的应用之动态代理
package com.william.java.reflection.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
//动态代理接口的使用, 体会反射是动态语言的关键
interface Subject {
void action();
}
// 被代理类
class RealSubject implements Subject {
@Override
public void action() {
System.out.println("我是被代理类,记得要执行我哦^=^");
}
}
//
class MyInvocationHandler implements InvocationHandler {
Object obj;// 实现了接口的被代理类的对象的声明
// 1.给被代理的对象实例化
// 2.返回一个代理类的对象
public Object blind(Object obj) {
this.obj = obj;
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), this);
}
// 当通过代理类的对象发起对被重写的方法的调用时,都会转换为对如下的 invoke 方法的调用
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object returnVal = method.invoke(obj, args);
return returnVal;
}
}
public class TestProxy {
public static void main(String[] args) {
// 1.被代理类的对象
RealSubject realSubject = new RealSubject();
// 2.创建一个实现了 InvocationHandler 接口的类的对象
MyInvocationHandler handler = new MyInvocationHandler();
// 3.调用 blind() 方法,动态的返回一个同样实现了 real 所在类实现的接口 Subject 的代理类的对象
Object object = handler.blind(realSubject);
Subject subject = (Subject) object;// 此时 sub 就是代理类的对象
subject.action();// 转到对 InvacationHandler 接口的实现类的 invoke() 方法的调用
}
}
通过以下代码加深 动态代理的理解
package com.william.java.reflection.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
interface Person {
void say();
void eat();
}
interface Animal {
void roar();
void eat();
}
class TangWei implements Person {
@Override
public void say() {
System.out.println("I'm TangWei, Hello");
}
@Override
public void eat() {
System.out.println("TangWei eat ice cream");
}
}
class Angela implements Person {
@Override
public void say() {
System.out.println("I'm Angela, Hello");
}
@Override
public void eat() {
System.out.println("Angela eat ice cream");
}
}
class Cat implements Animal {
@Override
public void roar() {
System.out.println("I'm a Cat, miao~");
}
@Override
public void eat() {
System.out.println("Cat eat mouse");
}
}
class Dog implements Animal {
@Override
public void roar() {
System.out.println("I'm a Dog, wang~");
}
@Override
public void eat() {
System.out.println("Dog eat bone");
}
}
class MyInvHandler implements InvocationHandler {
Object object;
public Object blind(Object object) {
this.object = object;
return Proxy.newProxyInstance(object.getClass().getClassLoader(), object.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
return method.invoke(object, args);
}
}
public class test {
public static void main(String[] args) {
TangWei tangWei = new TangWei();
Angela angela = new Angela();
Cat cat = new Cat();
Dog dog = new Dog();
MyInvHandler handler = new MyInvHandler();
Person person = (Person) handler.blind(tangWei);
person.say();
person.eat();
person = (Person) handler.blind(angela);
person.say();
person.eat();
Animal animal = (Animal) handler.blind(cat);
animal.eat();
animal.roar();
animal = (Animal) handler.blind(dog);
animal.eat();
animal.roar();
}
}