OnJava8-Examples/typeinfo/SimpleProxyDemo.java

53 lines
1.2 KiB
Java
Raw Normal View History

2015-04-20 15:36:01 -07:00
//: typeinfo/SimpleProxyDemo.java
2015-05-29 14:18:51 -07:00
// <20>2015 MindView LLC: see Copyright.txt
2015-04-20 15:36:01 -07:00
import static net.mindview.util.Print.*;
interface Interface {
void doSomething();
void somethingElse(String arg);
}
class RealObject implements Interface {
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void doSomething() { print("doSomething"); }
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void somethingElse(String arg) {
print("somethingElse " + arg);
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
class SimpleProxy implements Interface {
private Interface proxied;
public SimpleProxy(Interface proxied) {
this.proxied = proxied;
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void doSomething() {
print("SimpleProxy doSomething");
proxied.doSomething();
}
2015-05-05 11:20:13 -07:00
@Override
2015-04-20 15:36:01 -07:00
public void somethingElse(String arg) {
print("SimpleProxy somethingElse " + arg);
proxied.somethingElse(arg);
}
2015-05-18 23:05:20 -07:00
}
2015-04-20 15:36:01 -07:00
class SimpleProxyDemo {
public static void consumer(Interface iface) {
iface.doSomething();
iface.somethingElse("bonobo");
}
public static void main(String[] args) {
consumer(new RealObject());
consumer(new SimpleProxy(new RealObject()));
}
} /* Output:
doSomething
somethingElse bonobo
SimpleProxy doSomething
doSomething
SimpleProxy somethingElse bonobo
somethingElse bonobo
*///:~