大家好,又见面了,我是你们的朋友全栈君。
Visitor是访问者的意思。 数据结构中保存着元素。一般我们需要对元素进行处理,那么处理元素的代码放在哪里呢?最显然的方法就是放在数据结构的类中,在类中添加处理的方法。但是如果有很多处理,就比较麻烦了,每当增加一种处理,我们就不得不去修改表示数据结构的类。 visitor模式就是用来解决这个问题的,visitor模式将数据结构的定义和处理分离开。也就是会新增一个访问者的类,将数据元素的处理交给访问者类,这样以后要新增处理的时候,只需要新增访问者就可以了。
将更新(变更)封装到一个类中(访问操作),并由待更改类提供一个接收接口,则可达到效果。
//visit.h
#ifndef VISITOR_H
#define VISITOR_H
#include <iostream>
#include <string>
#include <vector>
class Element;
class CPU;
class VideoCard;
class MainBoard;
/*------------------*/
class Visitor {
public:
Visitor(std::string name) {
visitorName = name;
}
virtual void visitCPU( CPU* cpu ) {
};
virtual void visitVideoCard( VideoCard* videoCard ) {
};
virtual void visitMainBoard( MainBoard* mainBoard ) {
};
std::string getName() {
return this->visitorName;
};
private:
std::string visitorName;
};
class Element {
public:
Element( std::string name ) {
eleName = name;
}
virtual void accept( Visitor* visitor ) {
};
virtual std::string getName() {
return this->eleName;
}
private:
std::string eleName;
};
/*----------- Elements -------------*/
class CPU : public Element {
public:
CPU(std::string name) : Element(name) {
}
void accept(Visitor* visitor) {
visitor->visitCPU(this);
}
};
class VideoCard : public Element {
public:
VideoCard(std::string name) : Element(name) {
}
void accept(Visitor* visitor) {
visitor->visitVideoCard(this);
}
};
class MainBoard : public Element {
public:
MainBoard(std::string name) : Element(name) {
}
void accept(Visitor* visitor) {
visitor->visitMainBoard(this);
}
};
/*----------- ConcreteVisitor -------------*/
class CircuitDetector : public Visitor {
public:
CircuitDetector(std::string name) : Visitor(name) {
}
// checking cpu
void visitCPU( CPU* cpu ) {
std::cout << Visitor::getName() << " is checking CPU's circuits.(" << cpu->getName()<<")" << std::endl;
}
// checking videoCard
void visitVideoCard( VideoCard* videoCard ) {
std::cout << Visitor::getName() << " is checking VideoCard's circuits.(" << videoCard->getName()<<")" << std::endl;
}
// checking mainboard
void visitMainBoard( MainBoard* mainboard ) {
std::cout << Visitor::getName() << " is checking MainBoard's circuits.(" << mainboard->getName() <<")" << std::endl;
}
};
class FunctionDetector : public Visitor {
public:
FunctionDetector(std::string name) : Visitor(name) {
}
virtual void visitCPU( CPU* cpu ) {
std::cout << Visitor::getName() << " is check CPU's function.(" << cpu->getName() << ")"<< std::endl;
}
// checking videoCard
void visitVideoCard( VideoCard* videoCard ) {
std::cout << Visitor::getName() << " is checking VideoCard's function.(" << videoCard->getName()<< ")" << std::endl;
}
// checking mainboard
void visitMainBoard( MainBoard* mainboard ) {
std::cout << Visitor::getName() << " is checking MainBoard's function.(" << mainboard->getName() << ")"<< std::endl;
}
};
/*------------------------*/
class Computer {
public:
Computer(CPU* cpu,
VideoCard* videocard,
MainBoard* mainboard) {
elementList.push_back(cpu);
elementList.push_back(videocard);
elementList.push_back(mainboard);
};
void Accept(Visitor* visitor) {
for( std::vector<Element*>::iterator i = elementList.begin(); i != elementList.end(); i++ )
{
(*i)->accept(visitor);
}
};
private:
std::vector<Element*> elementList;
};
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/168496.html原文链接:https://javaforall.cn