Skip to content

Instantly share code, notes, and snippets.

@chelseatroy
Last active November 3, 2019 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chelseatroy/a01e665771eff865662b39f3a5ed498d to your computer and use it in GitHub Desktop.
Save chelseatroy/a01e665771eff865662b39f3a5ed498d to your computer and use it in GitHub Desktop.
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