資料內(nèi)容:
(3)定義小小紅代理類,她需要代購去售賣香奈兒香水,所以她是香奈兒香水提供商的代理對象,同樣實現(xiàn)接口,并在內(nèi)部保存對目標(biāo)對象(香奈兒提供商)的引用,控制其它對象對目標(biāo)對象的訪問。
public class XiaoHongSellProxy implements SellPerfume {
private SellPerfume sellPerfumeFactory;
public XiaoHongSellProxy(SellPerfume sellPerfumeFactory) {
this.sellPerfumeFactory = sellPerfumeFactory;
}
@Override
public void sellPerfume(double price) {
doSomethingBeforeSell(); // 前置增強(qiáng)
sellPerfumeFactory.sellPerfume(price);
doSomethingAfterSell(); // 后置增強(qiáng)
}
private void doSomethingBeforeSell() {
System.out.println("小紅代理購買香水前的額外操作...");
}
private void doSomethingAfterSell() {
System.out.println("小紅代理購買香水后的額外操作...");
}
}