资料内容:
(3)定义小小红代理类,她需要代购去售卖香奈儿香水,所以她是香奈儿香水提供商的代理对象,同样实现接口,并在内部保存对目标对象(香奈儿提供商)的引用,控制其它对象对目标对象的访问。
public class XiaoHongSellProxy implements SellPerfume {
private SellPerfume sellPerfumeFactory;
public XiaoHongSellProxy(SellPerfume sellPerfumeFactory) {
this.sellPerfumeFactory = sellPerfumeFactory;
}
@Override
public void sellPerfume(double price) {
doSomethingBeforeSell(); // 前置增强
sellPerfumeFactory.sellPerfume(price);
doSomethingAfterSell(); // 后置增强
}
private void doSomethingBeforeSell() {
System.out.println("小红代理购买香水前的额外操作...");
}
private void doSomethingAfterSell() {
System.out.println("小红代理购买香水后的额外操作...");
}
}