interface AppService {
void play(String kkk);
}
class MyApp implements AppService{
@Override
public void play(String kkk) {
System.out.println("play "+ kkk);
}
}
interface Car{
String run(String speed);
}
class BwmCar implements Car{
@Override
public String run(String speed) {
System.out.println("run speed " +speed);
return speed;
}
}
class LoggerInterceptor implements InvocationHandler {
private Object target; //委托类(被代理类)的实例,比如厂家
public LoggerInterceptor(Object target){
this.target = target;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)throws Throwable {
System.out.println("Entered "+target.getClass().getName()+"-"+method.getName()+",with arguments{"+args[0]+"}");
Object result = method.invoke(target, args);
//调用目标对象的方法 (调用厂家的方法(createApp)及参数(Kevin Test))
System.out.println("Before return:"+result);
return result;
}
}
运行结果:
Entered com.winway.modules.wechat.MyApp-play,with arguments{music}
play music
Before return:null
Entered com.winway.modules.wechat.BwmCar-run,with arguments{300}
run speed 300
Before return:300