Intent
Represent an operation to be performed on the elements of an object structure.Visitor lets you define a new operation without changing the classes of the elements
on which it operates.
// DesignPattern.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//#include <iostream>
#include <share.h>
#include <list>
#include <memory>#define s std::cout
#define d std::endl;// Visitor Patternclass AssignmentNode;
class VariableRefNode;class NodeVisitor {
public:virtual void visitAssignment(std::shared_ptr<AssignmentNode> n) = 0;virtual void visitVariableRef(std::shared_ptr<VariableRefNode> n) = 0;
};class AssignmentVisitor : public NodeVisitor
{
public:AssignmentVisitor() {}~AssignmentVisitor() {}virtual void visitAssignment(std::shared_ptr<AssignmentNode> n){std::cout << typeid(this).name() << ":" << "Wa o, nice define a new operation to visit the" << std::endl;std::cout << typeid(this).name() << ":" << "classes of the elements without chinging" << std::endl;}virtual void visitVariableRef(std::shared_ptr<VariableRefNode> n){}
};class VariableRef : public NodeVisitor
{
public:VariableRef() {}~VariableRef() {}virtual void visitAssignment(std::shared_ptr<AssignmentNode> n){}virtual void visitVariableRef(std::shared_ptr<VariableRefNode> n){std::cout << typeid(this).name() << ":" << "Bingo, nice define a new operation to visit the" << std::endl;std::cout << typeid(this).name() << ":" << "classes of the elements without chinging" << std::endl;}
};class Node {
public:virtual void Accept(std::shared_ptr<NodeVisitor> v) {}virtual bool operator==(const Node& a) { return false; }std::string mem_;
};class AssignmentNode : public Node
{
public:AssignmentNode() {mem_ = std::string("AssignmentNode");}AssignmentNode(std::shared_ptr<AssignmentNode>){mem_ = std::string("AssignmentNode");}~AssignmentNode() {}virtual void Accept(std::shared_ptr<NodeVisitor> v){std::cout << typeid(v).name() << std::endl;std::cout << "visit succussefully AssignmentNode" << std::endl;v->visitAssignment(std::shared_ptr<AssignmentNode>(this));}virtual bool operator==(const Node& a){if (a.mem_.compare(mem_) == 0)return true;elsereturn false;}};class VariableRefNode : public Node
{
public:VariableRefNode() {mem_ = std::string("VariableRefNode");}~VariableRefNode() {}virtual void Accept(std::shared_ptr<NodeVisitor> v){std::cout << typeid(v).name() << std::endl;std::cout << "visit succussefully VariableRefNode" << std::endl;v->visitVariableRef(std::shared_ptr<VariableRefNode>(this));}virtual bool operator==(const Node& a){if (a.mem_.compare(mem_) == 0)return true;elsereturn false;}
};class Application
{
public:Application() {node_visitor_a_ = std::make_shared< AssignmentVisitor>();node_visitor_v_ = std::make_shared< VariableRef>();}~Application() {}void createNode(){s << "start work" << dstd::shared_ptr<Node> node = std::make_shared< AssignmentNode>();node_list_.push_back(node);std::shared_ptr<Node> node1 = std::make_shared< VariableRefNode>();node_list_.push_back(node1);}void traverseNode(){for (const auto& item : node_list_){AssignmentNode a_node;VariableRefNode v_node;if ((*item) == a_node){item->Accept(node_visitor_a_);}if ((*item) == v_node){item->Accept(node_visitor_v_);}}}void work(){createNode();traverseNode();}
private:std::list<std::shared_ptr<Node> > node_list_{};std::shared_ptr<NodeVisitor> node_visitor_a_;std::shared_ptr<NodeVisitor> node_visitor_v_;
};int main()
{std::shared_ptr<Application> a = std::make_shared<Application>();a->work();
}