Wife.java:
package com.cisco.gendwang;
public interface Wife
{
void cook();
}
MyWife.java:
package com.cisco.gendwang;
public class MyWife implements Wife
{
public MyWife()
{
}
public void cook()
{
System.out.println("My wife starts cooking");
}
}
CommonProxy.java
package com.cisco.gendwang;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class CommonProxy implements InvocationHandler
{
private Object proxyObj;
private CommonProxy(Object obj)
{
proxyObj = obj;
}
public static Object getProxy(Object obj)
{
Class cls = obj.getClass();
return Proxy.newProxyInstance(cls.getClassLoader(), cls.getInterfaces(), new CommonProxy(obj));
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
{
System.out.println("My Wife turns on the radio");
Object result = method.invoke(proxyObj, args);
System.out.println("My Wife turns off the radio");
return result;
}
}
Test.java:
package com.cisco.gendwang;
public class Test
{
public static void main(String[] args)
{
Wife wife = (Wife)CommonProxy.getProxy(new MyWife());
wife.cook();
}
}