public class Proxy {
//通过组合的方式 开闭原则 public static void main(String agrs[]) { Greeting greeting = new GreetProxy(new GreetingImpl()); greeting.sayHello("king"); }
}
interface Greeting {
void sayHello(String name);
}
class GreetingImpl implements Greeting {
public void sayHello(String name) { System.out.println("hello " + name); }
}
class GreetProxy implements Greeting {
GreetingImpl gi; public GreetProxy(GreetingImpl impl) { gi = impl; } public void sayHello(String name) { before(); //相当于AOP中的前置增强 gi.sayHello(name); } private void before() { System.out.println("before ..."); }
}