Skip to content

Instantly share code, notes, and snippets.

@claudemartin
Created August 22, 2017 16:48
Show Gist options
  • Save claudemartin/b341cb919eda99248baeaa29b36a419e to your computer and use it in GitHub Desktop.
Save claudemartin/b341cb919eda99248baeaa29b36a419e to your computer and use it in GitHub Desktop.
public final class Child {
private final Object token;
private final SomeClass parent;
Child(SomeClass parent, Object token) {
this.parent = parent;
this.token = token;
}
public void doStuff() {
parent.forChildrenOnly(token);
}
}
public final class SomeClass {
private final Object token = new Object();
void forChildrenOnly(Object token) {
if(token != this.token)
throw new IllegalAccessError("wrong token!");
System.out.println("success");
}
Child createChild() {
return new Child(this, token);
}
public static void main(String[] args) {
SomeClass parent = new SomeClass();
Child goodChild = parent.createChild();
Child badChild = new Child(parent, new Object());
goodChild.doStuff();
badChild.doStuff();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment