為某對象提供一種代理以控制對該對象的訪問阳似。即客戶端通過代理間接地訪問該對象叨粘,從而限制晤揣、增強或修改該對象的一些特性屋确。
- 靜態(tài)代理:需要定義接口或者父類纳击,被代理對象與代理對象一起實現(xiàn)相同的接口或者繼承相同父類
- 動態(tài)代理:
- JDK代理:代理接口;
- Cglib代理:不需要接口乍恐,而是創(chuàng)建子類
1评疗、靜態(tài)代理
package com.strife.pattern.proxy;
/**
* 代理模式:靜態(tài)代理
*
* @author mengzhenghao
* @date 2022/6/1
*/
public class StaticProxy {
public static void main(String[] args) {
//目標對象
TeacherDao teacherDao = new TeacherDao();
//通過代理對象調用被代理對象的方法
new TeacherDaoProxy(teacherDao).teach();
}
}
interface ITeacherDao {
void teach();
}
class TeacherDao implements ITeacherDao {
@Override
public void teach() {
System.out.println("teach方法被調用……");
}
}
class TeacherDaoProxy implements ITeacherDao {
private TeacherDao traget;
public TeacherDaoProxy(TeacherDao traget) {
this.traget = traget;
}
@Override
public void teach() {
System.out.println("teach()方法執(zhí)行之前");
traget.teach();
System.out.println("teach()方法執(zhí)行之后");
}
}
2、JDK動態(tài)代理
package com.strife.pattern.proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 代理模式:JDK動態(tài)代理
*
* @author mengzhenghao
* @date 2022/6/1
*/
public class JdkProxy {
public static void main(String[] args) {
StudentDao studentDao = new StudentDao();
ProxyFactory factory = new ProxyFactory(studentDao);
IStudentDao proxyObject = (IStudentDao) factory.getProxyInstance();
//class com.strife.pattern.proxy.$Proxy0 內存中動態(tài)生成了代理對象
System.out.println(proxyObject.getClass());
proxyObject.study();
}
}
interface IStudentDao {
void study();
}
class StudentDao implements IStudentDao {
@Override
public void study() {
System.out.println("studying……");
}
}
class ProxyFactory {
private StudentDao target;
public ProxyFactory(StudentDao target) {
this.target = target;
}
/** 獲取代理對象 */
public Object getProxyInstance() {
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new InvocationHandler() {
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("JDK代理開始");
return method.invoke(target, args);
}
});
}
}
3茵烈、Cglib動態(tài)代理
package com.strife.pattern.proxy;
import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;
/**
* 代理對象:Cglib動態(tài)代理
*
* @author mengzhenghao
* @date 2022/6/1
*/
public class CglibProxy {
public static void main(String[] args) {
People people = new People();
final People peopleProxy = (People) new PeopleProxyFactory(people).getProxyInstance();
System.out.println(peopleProxy.walk());
}
}
class People {
String walk() {
System.out.println("walking……");
return "OK";
}
}
class PeopleProxyFactory implements MethodInterceptor {
private Object target;
public PeopleProxyFactory(Object target) {
this.target = target;
}
/** 返回代理對象 */
public Object getProxyInstance() {
//創(chuàng)建工具類
final Enhancer enhancer = new Enhancer();
//設置父類
enhancer.setSuperclass(target.getClass());
//設置回調函數(shù)
enhancer.setCallback(this);
//創(chuàng)建代理對象
return enhancer.create();
}
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("Cglib代理模式開始");
return method.invoke(target, args);
}
}
<!--Cglib依賴-->
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.1</version>
</dependency>