目录
1、代理模式与动态代理
●代理设计模式的原理:
使用一个代理将对象包装起来,然后用该代理对象取代原始对象。任何对原始对象的调用都要通过代理。代理对象决定是否以及何时将方法调用转到原始对象上。
●代理机制的操作,属于静态代理,特征是代理类和目标对象的类都是在编译期间确定下来,不利于程序的扩展。同时,每一个代理类只能为一个接口服务,这样一来程序开发中必然产生过多的代理。最好可以通过一个代理类完成全部的代理功能。
●动态代理是指客户通过代理类来调用其它对象的方法,并且是在程序运行时根据需要动态创建目标类的代理对象。
●动态代理使用场合:
调试
远程方法调用
●动态代理相比于静态代理的优点:
抽象角色中(接口)声明的所有方法都被转移到调用处理器一个集中的方法中处理,这样,我 们可以更加灵活和统一的处理众多的方法。
2、静态代理举例
/**
* @Author zhang
* @Date 2021/11/11 21:42
* @Version 1.0
*/
//静态代理举例
//特点:代理类和被代理类在编译期间,就确定下来了。
interface ClothFactory{
void productCloth();
}
//代理类
class ProxyClothFactory implements ClothFactory{
private ClothFactory factory;//用被代理对象进行实例化
public ProxyClothFactory(ClothFactory factory){
this.factory = factory;
}
@Override
public void productCloth() {
System.out.println("代理工厂准备");
factory.productCloth();
System.out.println("代理工厂收尾");
}
}
class NikeClothFactory implements ClothFactory{
@Override
public void productCloth() {
System.out.println("Nike工厂生产衣服");
}
}
public class Main {
public static void main(String[] args) {
//创建被代理对象
ClothFactory nike = new NikeClothFactory();
//创建代理对象
ProxyClothFactory proxyClothFactory = new ProxyClothFactory(nike);
proxyClothFactory.productCloth();
//代理工厂准备
//Nike工厂生产衣服
//代理工厂收尾
}
}
3、动态代理举例
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @Author zhang
* @Date 2021/11/11 21:42
* @Version 1.0
*/
//动态代理举例
interface ClothFactory{
void productCloth();
}
//代理类
class ProxyClothFactory implements ClothFactory{
private ClothFactory factory;//用被代理对象进行实例化
public ProxyClothFactory(ClothFactory factory){
this.factory = factory;
}
@Override
public void productCloth() {
System.out.println("代理工厂准备");
factory.productCloth();
System.out.println("代理工厂收尾");
}
}
class NikeClothFactory implements ClothFactory{
@Override
public void productCloth() {
System.out.println("Nike工厂生产衣服");
}
}
interface Human {
String getBelief();
void eat(String food);
}
//被代理类
class SuperHuman implements Human {
@Override
public String getBelief() {
return "I believe I can fly!";
}
@Override
public void eat(String food) {
System.out.println("I like eating " + food);
}
}
/*
要想实现动态代理,需要解决的问题?
问题一:如何根据加载到内存中的被代理类,动态的创建一个代理类及其对象。
问题二:当通过代理类的对象调用方法a时,如何动态的去调用被代理类中的同名方法a。
*/
class ProxyFactory {
//调用此方法返回一个代理类的对象
public static Object getProxyInstance(Object obj) {
MyInvocationHandler handler = new MyInvocationHandler();
handler.bind(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
}
}
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:被代理类的对象
return method.invoke(obj, args);
}
}
public class Main {
public static void main(String[] args) {
SuperHuman sh = new SuperHuman();
Human proxyInstance = (Human) ProxyFactory.getProxyInstance(sh);
//当通过代理类对象调用方法时,会自动调用被代理类中同名的对象
String belief = proxyInstance.getBelief();
System.out.println(belief);
proxyInstance.eat("bread");
//I believe I can fly!
//I like eating bread
System.out.println("------------------------------------------------");
NikeClothFactory nikeClothFactory = new NikeClothFactory();
ClothFactory nike = (ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory);
nike.productCloth();
//Nike工厂生产衣服
}
}
4、AOP与动态代理
Proxy和InvocationHandler,很难看出这种动态代理的优势,下面介绍一种更实用的动态代理机制
>改进后的说明:代码段1、代码段2、代码段3和深色代码段分离开了,但代码段1、2、3又和一个特定的方法A耦合了!最理想的效果是:代码块1、2、3既可以执行方法A,又无须在程序中以硬编码的方式直接调用深色代码的方法
>使用Proxy生成一个动态代理时,往往并不会凭空产生一个动态代理,这样没有太大的意义。通常都是为指定的目标对象生成动态代理
>这种动态代理在AOP中被称为AOP代理,AOP代理可代替目标对象,AOP代理包含了目标对象的全部方法。但AOP代理中的方法与目标对象的方法存在差异:AOP代理里的方法可以在执行目标方法之前、之后插入一些通用处理。
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* @Author zhang
* @Date 2021/11/11 21:42
* @Version 1.0
*/
//动态代理举例
interface ClothFactory {
void productCloth();
}
//代理类
class ProxyClothFactory implements ClothFactory {
private ClothFactory factory;//用被代理对象进行实例化
public ProxyClothFactory(ClothFactory factory) {
this.factory = factory;
}
@Override
public void productCloth() {
System.out.println("代理工厂准备");
factory.productCloth();
System.out.println("代理工厂收尾");
}
}
class NikeClothFactory implements ClothFactory {
@Override
public void productCloth() {
System.out.println("Nike工厂生产衣服");
}
}
interface Human {
String getBelief();
void eat(String food);
}
//被代理类
class SuperHuman implements Human {
@Override
public String getBelief() {
return "I believe I can fly!";
}
@Override
public void eat(String food) {
System.out.println("I like eating " + food);
}
}
/*
要想实现动态代理,需要解决的问题?
问题一:如何根据加载到内存中的被代理类,动态的创建一个代理类及其对象。
问题二:当通过代理类的对象调用方法a时,如何动态的去调用被代理类中的同名方法a。
*/
class ProxyFactory {
//调用此方法返回一个代理类的对象
public static Object getProxyInstance(Object obj) {
MyInvocationHandler handler = new MyInvocationHandler();
handler.bind(obj);
return Proxy.newProxyInstance(obj.getClass().getClassLoader(), obj.getClass().getInterfaces(), handler);
}
}
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:被代理类的对象
HumanUtil humanUtil = new HumanUtil();
humanUtil.method1();
Object invoke = method.invoke(obj, args);
humanUtil.method2();
return invoke;
}
}
class HumanUtil {
public void method1() {
System.out.println("--------通用方法1--------");
}
public void method2() {
System.out.println("--------通用方法2--------");
}
}
public class Main {
public static void main(String[] args) {
SuperHuman sh = new SuperHuman();
Human proxyInstance = (Human) ProxyFactory.getProxyInstance(sh);
//当通过代理类对象调用方法时,会自动调用被代理类中同名的对象
String belief = proxyInstance.getBelief();
System.out.println(belief);
proxyInstance.eat("bread");
System.out.println("------------------------------------------------");
NikeClothFactory nikeClothFactory = new NikeClothFactory();
ClothFactory nike = (ClothFactory) ProxyFactory.getProxyInstance(nikeClothFactory);
nike.productCloth();
//--------通用方法1--------
//--------通用方法2--------
//I believe I can fly!
//--------通用方法1--------
//I like eating bread
//--------通用方法2--------
//------------------------------------------------
//--------通用方法1--------
//Nike工厂生产衣服
//--------通用方法2--------
}
}