其他分享
首页 > 其他分享> > 05.【设计模式】代理模式

05.【设计模式】代理模式

作者:互联网

1.定义

2.应用场景

3.代理模式的通用写法

public interface ISubject {
    void request();
}
public class RealSubject implements ISubject{
    @Override
    public void request() {
        System.out.println("real service is called");
    }
}
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