-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junzhuo Du <[email protected]>
- Loading branch information
Junzhuo Du
committed
Mar 1, 2020
1 parent
2711fde
commit 8d2fb19
Showing
2 changed files
with
111 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
/* | ||
* C++ Design Patterns: | ||
* Author: Junzhuo Du [github.com/Junzhuodu] | ||
* 2020 | ||
* | ||
*/ | ||
|
||
#include <iostream> | ||
|
||
class Element; | ||
class ConcreteElementA; | ||
class ConcreteElementB; | ||
|
||
class Vistor { | ||
public: | ||
virtual ~Vistor() {} | ||
|
||
virtual void vistorElementA(ConcreteElementA* const element) = 0; | ||
virtual void vistorElementB(ConcreteElementB* const element) = 0; | ||
}; | ||
|
||
class ConcreteVistor1 : public Vistor { | ||
public: | ||
~ConcreteVistor1() {} | ||
|
||
void vistorElementA(ConcreteElementA* const element); | ||
void vistorElementB(ConcreteElementB* const element); | ||
}; | ||
|
||
class ConcreteVistor2 : public Vistor { | ||
public: | ||
~ConcreteVistor2() {} | ||
|
||
void vistorElementA(ConcreteElementA* const element); | ||
void vistorElementB(ConcreteElementB* const element); | ||
}; | ||
|
||
class Element { | ||
public: | ||
virtual ~Element() {} | ||
|
||
virtual void accept(Vistor& v) = 0; | ||
}; | ||
|
||
class ConcreteElementA : public Element { | ||
public: | ||
ConcreteElementA(const std::string& data) | ||
: data_(data) {} | ||
~ConcreteElementA() {} | ||
|
||
void accept(Vistor& v) { | ||
v.vistorElementA(this); | ||
} | ||
|
||
std::string getData() { | ||
return data_; | ||
} | ||
private: | ||
std::string data_; | ||
}; | ||
|
||
class ConcreteElementB : public Element { | ||
public: | ||
ConcreteElementB(const std::string& data) | ||
: data_(data) {} | ||
~ConcreteElementB() {} | ||
|
||
void accept(Vistor& v) { | ||
v.vistorElementB(this); | ||
} | ||
|
||
std::string getData() { | ||
return data_; | ||
} | ||
private: | ||
std::string data_; | ||
}; | ||
|
||
void ConcreteVistor1::vistorElementA(ConcreteElementA *const element) { | ||
std::cout << "Concrete Vistor 1 : Element A visited." << std::endl; | ||
std::cout << "Element A data: " << element->getData() << std::endl; | ||
} | ||
|
||
void ConcreteVistor1::vistorElementB(ConcreteElementB *const element) { | ||
std::cout << "Concrete Vistor 1 : Element B visited." << std::endl; | ||
std::cout << "Element B data: " << element->getData() << std::endl; | ||
} | ||
|
||
void ConcreteVistor2::vistorElementA(ConcreteElementA *const element) { | ||
std::cout << "Concrete Vistor 2 : Element A visited." << std::endl; | ||
std::cout << "Element A data: " << element->getData() << std::endl; | ||
} | ||
|
||
void ConcreteVistor2::vistorElementB(ConcreteElementB *const element) { | ||
std::cout << "Concrete Vistor 2 : Element B visited." << std::endl; | ||
std::cout << "Element B data: " << element->getData() << std::endl; | ||
} | ||
|
||
int main() { | ||
ConcreteElementA elementA("Hello"); | ||
ConcreteElementB elementB("World"); | ||
|
||
ConcreteVistor1 vistor1; | ||
ConcreteVistor2 vistor2; | ||
|
||
elementA.accept(vistor1); | ||
elementA.accept(vistor2); | ||
|
||
elementB.accept(vistor1); | ||
elementB.accept(vistor2); | ||
} |