Created
August 22, 2017 16:48
-
-
Save claudemartin/b341cb919eda99248baeaa29b36a419e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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