Last active
November 3, 2019 14:21
-
-
Save chelseatroy/a01e665771eff865662b39f3a5ed498d to your computer and use it in GitHub Desktop.
Visitor Pattern
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
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