Java静态代理、动态代理以及AOP

静态代理示例代码:

/*
 * 静态代理:只适合具体的代理
 */
interface ClothFactory {
	void clothProduct();
}

class NikeFactory implements ClothFactory {
	@Override
	public void clothProduct() {
		System.out.println("Product Nike Sports.");
	}
}

class ProxyFactory implements ClothFactory {
	ClothFactory cf;
	
	//创建代理类对象时,实际传入一个被代理对象
	public ProxyFactory(ClothFactory cf) {
		this.cf = cf;
	}
	
	@Override
	public void clothProduct() {
		System.out.println("You need give $10000 to ProxyFactory");
		cf.clothProduct();
	}
}

public class TestClothProduct {
	public static void main(String[] args) {
		NikeFactory nf = new NikeFactory();//创建被代理对象
		ProxyFactory pf = new ProxyFactory(nf);//创建代理对象
		pf.clothProduct();
	}
}

动态代理示例代码:

/*
 * 动态代理:可通用所有的代理
 */

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("I am RealSubject!");
	}
}

class MyInvocationHandler implements InvocationHandler {
	Object obj;// 实现了接口的被代理类的对象的声明

	// ①给被代理的对象实例化②返回一个代理类的对象
	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);//method方法的返回值时returnVal
		
		return returnVal;
	}
}

public class TestProxy {
	public static void main(String[] args) {
		//1.被代理类的对象
		RealSubject real = new RealSubject();
		//2.创建一个实现了InvacationHandler接口的类的对象
		MyInvocationHandler handle = new MyInvocationHandler();
		//3.调用blind()方法,动态的返回一个同样实现了real所在类实现的接口Subject的代理类的对象。
		Object obj =  handle.blind(real);
		Subject subject = (Subject)obj;
		subject.action();
		
		//再举一例
		NikeFactory nf = new NikeFactory();
		ClothFactory cf = (ClothFactory) handle.blind(nf);//proxyCloth即为代理类的对象
		cf.clothProduct();
	}
}

AOP示例图:



AOP示例代码:

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

interface Human {
	void info();
	
	void fly();
}


//被代理类
class SuperMan implements Human {
	@Override
	public void info() {
		System.out.println("Call me SuperMan!");
	}

	@Override
	public void fly() {
		System.out.println("I can fly!");
	}
}

class HumanUtil {
	void method1() {
		System.out.println("==========方法一==========");
	}
	
	void method2() {
		System.out.println("==========方法二==========");
	}
}

class MyInvocationHandler2 implements InvocationHandler {
	Object obj;// 被代理类对象的声明
	
	public void setObject(Object obj) {
		this.obj = obj;
	}

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		HumanUtil h = new HumanUtil();
		h.method1();
		
		Object returnVal = method.invoke(obj, args);
		
		h.method2();
		
		return returnVal;
	}
}

class MyProxy {
	// 动态的创建一个代理类的对象
	public static Object getProxyInstance(Object obj) {
		MyInvocationHandler2 m = new MyInvocationHandler2();
		m.setObject(obj);
		return Proxy.newProxyInstance(obj.getClass().getClassLoader(), 
				obj.getClass().getInterfaces(), m);
	}
}

public class TestAOP {
	public static void main(String[] args) {
		SuperMan superMan = new SuperMan();//创建一个被代理类的对象
		Object obj = MyProxy.getProxyInstance(superMan);//返回一个代理类的对象
		Human human = (Human) obj;
		human.info();//通过代理类的对象调用重写的抽象方法
		System.out.println();
		human.fly();
		System.out.println();
		
		//*********
		NikeFactory nf = new NikeFactory();
		Object obj2 = MyProxy.getProxyInstance(nf);
		ClothFactory cf = (ClothFactory) obj2;
		cf.clothProduct();
	}
}


猜你喜欢

转载自blog.csdn.net/u013453970/article/details/48413207