- package demo2;
- interface IEat{
- void get();
- }
- class EatReal implements IEat{
- public void get() {
- System.out.println("[真实主题类] 得到一份食物, 然后开始品尝美味");
- }
- }
- class EatProxy implements IEat {
- private IEat eat;
- public EatProxy(IEat eat) {
- this.eat = eat;
- }
- public void prepare() {
- System.out.println("[代理主题类] 准备吃");
- }
- public void get() {
- this.prepare();
- this.eat.get();
- this.clean();
- }
- public void clean() {
- System.out.println("[代理主题类] 收拾碗筷");
- }
- }
- public class ProxyTest {
- public static void main(String[] args) {
- IEat eat = new EatProxy(new EatReal());
- eat.get();
- }
- }
来源: http://www.bubuko.com/infodetail-2923596.html