Java設計模式——代理模式

public interface People {
    public void work();
}

public class RealPeople implements People {
    public void work() {
        System.out.println("工做中...");
    }
}

/**
 * 代理類
 *
 */
public class Proxy implements People {

    private RealPeople realPeople;

    public Proxy(RealPeople realPeople) {
        this.realPeople = realPeople;
    }

    public void work() {
        System.out.println("before work...");
        realPeople.work();
        System.out.println("after work...");
    }
}

public class ProxyTest {
    public static void main(String[] args) {
        Proxy proxy = new Proxy(new RealPeople());
        proxy.work();
    }
}
相關文章
相關標籤/搜索