Last active
December 14, 2018 15:11
-
-
Save ysheng26/5ced842a80df31bcb0fca6ea757b4b18 to your computer and use it in GitHub Desktop.
This file contains 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
class A : class Base { | |
public: | |
void doSomething(Visitor& v) { | |
v.visit(this); | |
} | |
}; | |
class B : class Base { | |
public: | |
void doSomething(Visitor& v) { | |
v.visit(this); | |
} | |
}; | |
// Visitors hierarchy | |
class Visitor { | |
public: | |
virtual void visit(A* a) = 0; | |
virtual void visit(B* b) = 0; | |
}; | |
class Visitor1 : public Visitor { | |
public: | |
void visit(A* a) { | |
cout << "behavior of A + Visitor1" << endl; | |
} | |
void visit(B* b) { | |
cout << "behavior of B + Visitor1" << endl; | |
} | |
}; | |
class Visitor2 : public Visitor { | |
public: | |
void visit(A* a) { | |
cout << "behavior of A + Visitor2" << endl; | |
} | |
void visit(B* b) { | |
cout << "behavior of B + Visitor2" << endl; | |
} | |
}; | |
// ------------------------------------------------ | |
// client code | |
Visitor1 v1; | |
Visitor1 v2; | |
A a; | |
B b; | |
a.doSomething(v1); | |
b.doSomething(v1); | |
a.doSomething(v2); | |
b.doSomething(v2); | |
// ------------------------------------------------ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment