Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Visitor Pattern
interface BoxVisitor {
int visitBob(BobBox box); ​
int visitAlice(AliceBox box);
}
...
abstract class Box {
abstract void accept(BoxVisitor visitor);
}
class BobBox extends Box {
private int x1;
private int y1;
private int width;
private int height;
public BobBox(int x1, int width, int y1, int height) {
this.x1 = x1;
this.y1 = y1;
this.width = width;
this.height = height;
}
@Override
void accept(BoxVisitor visitor) {
visitor.visitBob(this);
}
}
class AliceBox extends Box {
private int x1;
private int x2;
private int y1;
private int y2;
public AliceBox(int x1, int x2, int y1, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
@Override
void accept(BoxVisitor visitor) {
visitor.visitAlice(this);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.