设计模式-代理模式

代理模式UML类图

代理模式,旨在代理某个对象的职责,并且可以在职责代理过程中,新增其他辅助方法,代理模式也叫作委托模式。

Subject抽象主题角色:被代理角色的抽象角色

RealSubject具体主题角色:被代理角色

Proxy代理角色:对被代理角色实现方法增强

一、JDK动态代理

JDK动态代理是基于反射原理实现,可以查看JDK提供的InvocationHandler接口和Proxy类

1、被代理对象抽象接口

package com.example.demo.proxy;

public interface SubjectInterface {
	void coreMethod();
}

2、具体被代理对象

package com.example.demo.proxy;

public class Subject implements SubjectInterface {
	@Override
	public void coreMethod() {
		System.out.println("具体角色");
	}
}

3、增强被代理对象

package com.example.demo.proxy;

public class Advice {
	public void before(){
		System.out.println("前置处理");
	}

	public void after(){
		System.out.println("后置处理");
	}
}

4、JDK动态代理测试

package com.example.demo.proxy;

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

public class JdkTest {
	public static void main(String[] args) {
		// 被代理对象
		Subject subject = new Subject();
		// 增强对象
		Advice advice = new Advice();
		InvocationHandler handler = new InvocationHandler() {
			@Override
			public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
				advice.before();
				Object invoke = method.invoke(subject, args);
				advice.after();
				return invoke;
			}
		};
		SubjectInterface proxy = (SubjectInterface) Proxy.newProxyInstance(
				subject.getClass().getClassLoader(), // 传入ClassLoader
				subject.getClass().getInterfaces(), // 传入要实现的接口
				handler); // 传入处理调用方法的InvocationHandler
		// 执行方法
		proxy.coreMethod();
	}
}

在运行期动态创建一个interface实例的方法如下:

  1. 定义一个InvocationHandler实例,它负责实现接口的方法调用
  2. 通过Proxy.newProxyInstance()创建interface实例,它需要3个参数:
    1. 使用的ClassLoader,通常就是接口类的ClassLoader
    2. 需要实现的接口数组,至少需要传入一个接口进去
    3. 用来处理接口方法调用的InvocationHandler实例
  3. 将返回的Object强制转型为接口

动态代理实际上是JVM在运行期动态创建class字节码并加载的过程

二、CGLIB动态代理

CGLIB是一个强大的高性能的代码生成包,通过CGLIB的依赖包,可以在程序运行的过程中动态地生成对象和接口,CGLIB底层基于字节码处理框架ASM来转换字节码并生成新的类

1、引入依赖

<!-- https://mvnrepository.com/artifact/cglib/cglib-nodep -->
<dependency>
    <groupId>cglib</groupId>
    <artifactId>cglib-nodep</artifactId>
    <version>3.3.0</version>
</dependency>

2、具体被代理对象

package com.example.demo.proxy;

public class CglibSubject {
	public void coreMethod() {
		System.out.println("具体角色");
	}
}

3、增强被代理对象

package com.example.demo.proxy;

public class Advice {
	public void before(){
		System.out.println("前置处理");
	}

	public void after(){
		System.out.println("后置处理");
	}
}

4、CGLIB动态代理测试

package com.example.demo.proxy;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class CglibTest {
	public static void main(String[] args) {
		// 被代理对象
		CglibSubject cglibSubject = new CglibSubject();
		// 增强对象
		Advice advice = new Advice();
		// 创建增强器
		Enhancer enhancer = new Enhancer();
		// 设置增强目标类
		enhancer.setSuperclass(cglibSubject.getClass());
		// 设置回调
		enhancer.setCallback(new MethodInterceptor() {
			@Override
			public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy)
					throws Throwable {
				advice.before();
				Object invoke = method.invoke(cglibSubject, args);
				advice.after();
				return invoke;
			}
		});
		// 创建代理,调用核心方法
		CglibSubject targetProxy = (CglibSubject) enhancer.create();
		targetProxy.coreMethod();
	}
}

三、Spring AOP动态代理

Spring 会根据当前被代理类是否实现了接口进行JDK动态代理和CGLIB动态代理的选择

详见DefaultAopProxyFactory类源码:

package org.springframework.aop.framework;

import java.io.Serializable;
import java.lang.reflect.Proxy;
import org.springframework.aop.SpringProxy;
import org.springframework.core.NativeDetector;
import org.springframework.util.ClassUtils;

public class DefaultAopProxyFactory implements AopProxyFactory, Serializable {
    private static final long serialVersionUID = 7930414337282325166L;

    public DefaultAopProxyFactory() {
    }

    public AopProxy createAopProxy(AdvisedSupport config) throws AopConfigException {
        if (NativeDetector.inNativeImage() || !config.isOptimize() && !config.isProxyTargetClass() && !this.hasNoUserSuppliedProxyInterfaces(config)) {
            return new JdkDynamicAopProxy(config);
        } else {
            Class<?> targetClass = config.getTargetClass();
            if (targetClass == null) {
                throw new AopConfigException("TargetSource cannot determine target class: Either an interface or a target is required for proxy creation.");
            } else {
                return (AopProxy)(!targetClass.isInterface() && !Proxy.isProxyClass(targetClass) && !ClassUtils.isLambdaClass(targetClass) ? 
                new ObjenesisCglibAopProxy(config) : new JdkDynamicAopProxy(config));
            }
        }
    }

    private boolean hasNoUserSuppliedProxyInterfaces(AdvisedSupport config) {
        Class<?>[] ifcs = config.getProxiedInterfaces();
        return ifcs.length == 0 || ifcs.length == 1 && SpringProxy.class.isAssignableFrom(ifcs[0]);
    }
}
  • 15
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java设计模式是一组经过实践验证的面向对象设计原则和模式,可以帮助开发人员解决常见的软件设计问题。下面是常见的23种设计模式: 1. 创建型模式(Creational Patterns): - 工厂方法模式(Factory Method Pattern) - 抽象工厂模式(Abstract Factory Pattern) - 单例模式(Singleton Pattern) - 原型模式(Prototype Pattern) - 建造者模式(Builder Pattern) 2. 结构型模式(Structural Patterns): - 适配器模式(Adapter Pattern) - 桥接模式(Bridge Pattern) - 组合模式(Composite Pattern) - 装饰器模式(Decorator Pattern) - 外观模式(Facade Pattern) - 享元模式(Flyweight Pattern) - 代理模式(Proxy Pattern) 3. 行为型模式(Behavioral Patterns): - 责任链模式(Chain of Responsibility Pattern) - 命令模式(Command Pattern) - 解释器模式(Interpreter Pattern) - 迭代器模式(Iterator Pattern) - 中介者模式(Mediator Pattern) - 备忘录模式(Memento Pattern) - 观察者模式(Observer Pattern) - 状态模式(State Pattern) - 策略模式(Strategy Pattern) - 模板方法模式(Template Method Pattern) - 访问者模式(Visitor Pattern) 4. 并发型模式(Concurrency Patterns): - 保护性暂停模式(Guarded Suspension Pattern) - 生产者-消费者模式(Producer-Consumer Pattern) - 读写锁模式(Read-Write Lock Pattern) - 信号量模式(Semaphore Pattern) - 线程池模式(Thread Pool Pattern) 这些设计模式可以根据问题的特点和需求来选择使用,它们提供了一些可复用的解决方案,有助于开发高质量、可维护且易于扩展的软件系统。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值