public class JavaProxyDemo {
public static void main(String[] args) {
EatProxy eatProxy = new EatProxy(new EatReal());
eatProxy.eat();
}
}
interface IEat{
public void eat();
}
class EatReal implements IEat{
public void eat(){
System.out.println("[吃饭过程核心]");
}
}
class EatProxy implements IEat{
private IEat eat;
public EatPorxy(IEat eat){
this.eat = eat;
}
public void foot(){
System.out.println("[吃饭之前准备食材]");
}
public void clear(){
System.out.println("[吃完饭清理]");
}
public void eat(){
this.foot();
this.eat.eat();
this.clear();
}
}
如果你喜欢,可以关注我的公众号


本文介绍了一个使用Java代理模式的具体示例。通过定义EatReal和EatProxy两个类,实现了在核心功能执行前后添加额外操作的过程。EatReal类实现IEat接口,提供核心的吃东西功能;EatProxy类同样实现IEat接口,但在调用eat方法前调用foot方法准备食材,之后调用EatReal的eat方法,最后调用clear方法进行清理。

8943

被折叠的 条评论
为什么被折叠?



