首页 > 代码库 > Java 代理

Java 代理

参考文章

代理模式(Proxy):为其他对象提供一种代理以控制对这个对象的访问。

 

技术分享

 

abstract class Subject{ 
  public abstract void request(); 
} 

class RealSubject extends Subject{
    public void request() {
        System.out.println("This is real subject.");
    }
}

class Proxy extends Subject{
    private Subject subject = null;
    
    public Proxy(Subject subject) {
        this.subject = subject;
    }
    public void request() {
        System.out.println("Proxy calls real subject.");
        subject.request();
    }
}

public class StaticProxyTest {

    public static void main(String[] args) {
        Subject proxy = new Proxy(new RealSubject());
        proxy.request();
    }
}

 

Java 代理