package day9;
/**
* 以去饭堂吃饭为例子
* 代理设计模式的初步了解
* */
public class TestProxy {
public static void main(String[] args) {
Canteen canteen=new Worker(new Client());
canteen.eat();
}
}
interface Canteen{
public void eat();
}
class Worker implements Canteen{//这是代理类,等于饭堂里面的工作人员,帮你煮饭,洗碗等
private Canteen client;
public Worker(Canteen client) {
this.client=client;
}
public void cook() {
System.out.println("阿姨煮饭了");
}
@Override
public void eat() {
this.cook();
this.client.eat();
this.wash();
}
private void wash() {//洗碗
System.out.println("阿姨洗碗了");
}
}
class Client implements Canteen{//这客户类,只负责吃
@Override
public void eat() {
System.out.println("选好菜式,吃饭了");
}
}
08-10
08-10
08-10
08-10