class ThisEscape {
private String state;
public ThisEscape(EventSource source) {
state = "obj not ready";
source.registerListener(new EventListener() {
public void onEvent(Event e) {
doSomething(e);
}
});
state = "obj constructed done";
}
void doSomething(Event e) {
if(state == null) {
throw new RuntimeException("obj has not finish constructing");
}
/*this object ready? it depends:
depends on which thread is faster:
ces.startSender();
new ThisEscapeExplorer(ces);
*/
}
interface EventSource {
void registerListener(EventListener e);
}
interface EventListener {
void onEvent(Event e);
}
interface Event {
}
}
class CopierEventSource implements ThisEscape.EventSource {
public CopierEventSource() {
Thread a = new Thread(new Runnable() {
@Override
public void run() {
sendEvent();
}});
this.a = a;
}
Thread a;
ThisEscape.EventListener el;
public void startSender() {a.start();}
@Override
public void registerListener(ThisEscape.EventListener e) {
el = e;
}
private void sendEvent() {
ThisEscape.Event e = new ThisEscape.Event() {
};
el.onEvent(e);
}
}
public class ThisEscapeExplorer extends ThisEscape{
EventListener el;
public ThisEscapeExplorer(CopierEventSource source) {
super(source);
this.el = source.el;
}
void doSomething(ThisEscape.Event e) {
super.doSomething(e);
System.out.println("this is: " + this);
System.out.println("this.el is: " + el.toString());
//el.onEvent(e);
}
public static void main(String[] args) {
CopierEventSource ces = new CopierEventSource();
ces.startSender();
new ThisEscapeExplorer(ces);
}
}
ThisEscapeExplorer.java
ThisEscapeExplorer.java