public interface IHello {
String hi(String key);
}
public class HelloStaticProxy implements IHello{
private IHello iHello;
public HelloStaticProxy(IHello iHello) {
this.iHello = iHello;
}
@Override
public String hi(String key) {
System.out.println(">>>>>start proxy start");
String result = iHello.hi(key);
System.out.println(">>>>>static proxy end");
return result;
}
}
public class HelloImpl1 implements IHello{
@Override
public String hi(String key) {
String str = "hello:" + key;
System.out.println("HelloImpl1! " + str);
return str;
}
}
public class HelloImpl2 implements IHello{
@Override
public String hi(String key) {
String str = "hello:" + key;
System.out.println("HelloImpl2! " + str);
return str;
}
}
public class DemoTest {
public static void main(String[] args) {
IHello helloProxy = new HelloStaticProxy(new HelloImpl1());
helloProxy.hi("world");
helloProxy = new HelloStaticProxy(new HelloImpl2());
helloProxy.hi("world");
}
}
java_代理模式_静态代理
于 2024-05-16 16:34:03 首次发布