05.【设计模式】代理模式
作者:互联网
1.定义
2.应用场景
3.代理模式的通用写法
- 首先创建代理主体角色ISubject类
public interface ISubject {
void request();
}
- 创建真实主体角色RealSubject
public class RealSubject implements ISubject{
@Override
public void request() {
System.out.println("real service is called");
}
}
- 创建代理主题角色Proxy类
public class Proxy implements ISubject{
private ISubject subject;
public Proxy(ISubject subject){
this.subject = subject;
}
@Override
public void request() {
before();
subject.request();
after();
}
private void after() {
System.out.println("called after request");
}
private void before() {
System.out.println("called before request");
}
}
- 客户端调用测试
public class Test {
public static void main(String[] args) {
Proxy proxy = new Proxy(new RealSubject());
proxy.request();
}
}
4.从静态代理到动态代理
4.1 静态代理
4.2 动态代理之JDK
4.2 动态代理之CGLib
标签:05,void,ISubject,request,代理,Proxy,设计模式,public 来源: https://blog.csdn.net/weixin_44707283/article/details/119119252