※自己類別
public interface Self { public void buyChair(); public void buyTable(); } -------------------- public class SelfImpl implements Self { private String buyMan = "自己"; SelfImpl(String buyMan) { this.buyMan = buyMan; } @Override public void buyChair() { System.out.println(buyMan + "買椅子"); } @Override public void buyTable() { System.out.println(buyMan + "買桌子"); } }
※
※代理類別
public class Proxy implements Self { private Self proxyMan; Proxy(Self proxyMan) { this.proxyMan = proxyMan; } @Override public void buyChair() { proxyMan.buyChair(); } @Override public void buyTable() { proxyMan.buyTable(); } }
※
※測試
Self self = new SelfImpl("bruce"); self.buyChair(); self.buyTable(); System.out.println(); Self proxy = new Proxy(new SelfImpl("bruce")); proxy.buyChair(); proxy.buyTable();
※前三行不用代理;後三行使用代理
沒有留言:
張貼留言