版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82915013
静态代理是指在程序运行前代理关系就已经存在
其代理类和委托类会实现同一接口或是来自相同的父类
更多精彩
- 更多技术博客,请移步 asing1elife’s blog
定义共同接口
public class Sell {
void sell();
void ad();
}
定义委托类
public class Vendor implements Sell {
public void sell() {
System.out.println("In sell method");
}
public void ad() {
System.out.println("In ad method");
}
}
定义代理类
- 通过代理,可是实现对委托类被代理方法的修改,但不影响被代理方法的原始逻辑
public class BusinessAgent implements Sell {
private Vendor vendor;
public BusinessAgent(Vendor vendor) {
this.vendor = vendor;
}
public void sell() {
System.out.println("-- before --");
vendor.sell();
System.out.println("-- after --");
}
public void add() {
System.out.println("-- before --");
vendor.ad();
System.out.println("-- after --");
}
}