设计模式之代理模式
设计模式 Java About 3,609 words作用
为其他对象提供一种代理以控制对这个对象的访问。
静态代理
接口
public interface IBuyTicket {
void buy(int price);
}
被代理对象
public class BuyTicket implements IBuyTicket {
@Override
public void buy(int price) {
System.out.println("真实购票..." + price);
}
}
代理对象
public class BuyTicketProxy implements IBuyTicket {
private IBuyTicket iBuyTicket;
public BuyTicketProxy(IBuyTicket iBuyTicket) {
this.iBuyTicket = iBuyTicket;
}
@Override
public void buy(int price) {
System.out.println("代理购票开始");
iBuyTicket.buy(price);
System.out.println("代理购票结束");
}
}
调用
public class Client {
public static void main(String[] args) {
IBuyTicket iBuyTicket = new BuyTicketProxy(new BuyTicket());
iBuyTicket.buy(100);
}
}
动态代理
也叫JDK代理或接口代理。
接口
public interface IBuyTicket {
String buy(int price);
}
被代理对象
public class BuyTicket implements IBuyTicket {
@Override
public String buy(int price) {
return "购买成功:票价100";
}
}
代理工厂
public class ProxyFactory {
private Object target;
public ProxyFactory(Object 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代理开始");
Object invoke = method.invoke(target, args);
System.out.println("JDK代理结束");
return invoke;
}
});*/
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), (proxy, method, args) -> {
System.out.println("JDK代理开始");
Object invoke = method.invoke(target, args);
System.out.println("JDK代理结束");
return invoke;
});
}
}
调用
public class Client {
public static void main(String[] args) {
ProxyFactory factory = new ProxyFactory(new BuyTicket());
IBuyTicket proxyInstance = (IBuyTicket) factory.getProxyInstance();
proxyInstance.buy(200);
}
}
CGLIB代理
pom引用
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>3.3.0</version>
</dependency>
被代理对象
public class BuyTicket {
public String buy() {
return "Buy Success";
}
}
代理工厂
public class ProxyFactory implements MethodInterceptor {
private Object target;
public ProxyFactory(Object target) {
this.target = target;
}
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
System.out.println("cglib 开始");
Object invoke = method.invoke(target, args);
System.out.println("cglib 结束");
return invoke;
}
public Object getInstance() {
System.setProperty(DebuggingClassWriter.DEBUG_LOCATION_PROPERTY, "./");
//创建一个工具类
Enhancer enhancer = new Enhancer();
//设置父类
enhancer.setSuperclass(target.getClass());
//设置回调函数
enhancer.setCallback(this);
//创建子类对象,即代理对象
return enhancer.create();
}
}
调用
public class Client {
public static void main(String[] args) {
ProxyFactory proxyFactory = new ProxyFactory(new BuyTicket());
BuyTicket buyTicket = (BuyTicket) proxyFactory.getInstance();
buyTicket.buy();
}
}
源码
org.springframework.cglib.proxy.MethodInterceptor
Views: 2,050 · Posted: 2019-12-20
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓
Loading...