静态代理
编译期间,代理类和被代理类就已经确定
静态代理举例
interface ClothFactory {
void produceCloth();
}
//代理类
class ProxyClothFactory implements ClothFactory {
private ClothFactory factory;//用被代理类进行实例化
public ProxyClothFactory(ClothFactory factory) {
this.factory = factory;
}
@Override
public void produceCloth() {
System.out.println("代理工程在进行准备工作");
factory.produceCloth();
System.out.println("代理工厂收尾工作");
}
}
//被代理类1
class NikeCloth implements ClothFactory{
@Override
public void produceCloth() {
System.out.println("耐克工厂正在生产");
}
}
//被代理类2
class AdidasCloth implements ClothFactory{
@Override
public void produceCloth() {
System.out.println("阿迪达斯工厂正在生产");
}
}
public class StaticProxyTest {
public static void main(String[] args) {
//创建被代理类对象
NikeCloth nikeCloth = new NikeCloth();
AdidasCloth adidasCloth = new AdidasCloth();
//创建代理类对象
ProxyClothFactory proxyClothFactory1 = new ProxyClothFactory(nikeCloth);
ProxyClothFactory proxyClothFactory2 = new ProxyClothFactory(adidasCloth);
proxyClothFactory1.produceCloth();
proxyClothFactory2.produceCloth();
}
}
动态代理
编译期间,被代理类是不确定的,且没有实际定义的代理类
动态代理举例
接口和两个被代理类
interface Human {
String getBelief();
void eat(String food);
}
//被代理类
class Superman implements Human {
@Override
public String getBelief() {
return "I believe I can save the world!";
}
@Override
public void eat(String food) {
System.out.println(("Superman like eat\t" + food));
}
}
//被代理类
class NormalPeople implements Human {
@Override
public String getBelief() {
return "I believe I can be successful!";
}
@Override
public void eat(String food) {
System.out.println(("Normal People like eat\t" + food));
}
}
要实现动态代理,需要解决的问题
问题一:如何根据加载到内存中的被代理类,动态的创建一个代理类及其对象
问题二:当通过代理类的对象调用方法时,如何动态地调用被代理类中的同名方法
动态构造代理类
class ProxyFactory {
//调用此方法返回一个代理类的对象。解决问题一
public static Object getProxyInstance(Object obj) {//obj:被代理的对象
MyInvocationHandler handler = new MyInvocationHandler();
handler.bind(obj);
//Proxy.newProxyInstance()创建一个代理类对象
//参数一:被代理类的加载器;参数二:被代理类的接口;参数三:被代理类的
Object o = Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
return o;
}
}
InvocationHandler接口实现类
class MyInvocationHandler implements InvocationHandler {
private Object obj;//需要使用被代理类的对象进行赋值
public void bind(Object obj) {
this.obj = obj;
}
//当我们通过代理类的对象调用方法A时,就会自动调用如下的方法 invoke()
//将被代理类要执行的方法A要执行的功能声明在invoke()中
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//method:即为代理类对象调用的方法,此方法也就作为了被代理类要调用的方法
//obj:被代理类的对象
Object returnValue = method.invoke(obj, args);
//上述方法的返回值就作为当前类中的invoke()的返回值,
return returnValue;
}
}
main方法测试:
public static void main(String[] args) {
Human human = null;
//proxyInstance :代理类的对象
int m = 1;int n = 2;
for (int i = 0; i < 5; i++) {
int number = (int)Math.round(Math.random()*(n-m) + m);
switch (number){
case 1 :
human = new Superman();
break;
case 2 :
human = new NormalPeople();
break;
}
Human proxyInstance = (Human) ProxyFactory.getProxyInstance(human);
System.out.print(proxyInstance.getBelief());
proxyInstance.eat("hotpot");
}
}
输出:
I believe I can be successful!Normal People like eat hotpot
I believe I can save the world!Superman like eat hotpot
I believe I can be successful!Normal People like eat hotpot
I believe I can be successful!Normal People like eat hotpot
I believe I can save the world!Superman like eat hotpot
进一步理解动态代理可以参照AOP实现技术