Skip to content

Commit

Permalink
Create Vistor Pattern
Browse files Browse the repository at this point in the history
Signed-off-by: Junzhuo Du <[email protected]>
  • Loading branch information
Junzhuo Du committed Mar 1, 2020
1 parent 2711fde commit 8d2fb19
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 7 deletions.
7 changes: 0 additions & 7 deletions BehaviroalPatterns/visitor/Visitor.cpp

This file was deleted.

111 changes: 111 additions & 0 deletions BehaviroalPatterns/visitor/Visitor.cxx
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);
}

0 comments on commit 8d2fb19

Please sign in to comment.