QtDesignPatterns/visitorpattern/son.h

93 lines
1.8 KiB
C++

#include "father.h"
#include <vector>
#include <string>
class Keyboard : public ComputerPart
{
public:
void accept(ComputerPartVisitor *computerPartVisitor)
{
computerPartVisitor->visit(this);
}
string getString()
{
return "Displaying Keyboard";
}
};
class Monitor: public ComputerPart
{
public:
void accept(ComputerPartVisitor *computerPartVisitor)
{
computerPartVisitor->visit(this);
}
string getString()
{
return "Displaying Monitor";
}
};
class Mouse: public ComputerPart
{
public:
void accept(ComputerPartVisitor *computerPartVisitor)
{
computerPartVisitor->visit(this);
}
string getString()
{
return "Displaying Mouse";
}
};
class Computer: public ComputerPart
{
public:
Computer()
{
ComputerPart *keyboard = new Keyboard();
ComputerPart *monitor = new Monitor();
ComputerPart *mouse = new Mouse();
vector.push_back(keyboard);
vector.push_back(monitor);
vector.push_back(mouse);
}
void accept(ComputerPartVisitor *computerPartVisitor)
{
for(size_t i = 0; i < vector.size(); i++)
{
vector.at(i)->accept(computerPartVisitor);
}
computerPartVisitor->visit(this);
}
string getString()
{
return "Displaying Computer";
}
private:
std::vector<ComputerPart *> vector;
};
class ComputerPartDisplayVisitor: public ComputerPartVisitor
{
public:
void visit(Computer *computer)
{
cout << computer->getString() << endl;
}
void visit(Mouse *mouse)
{
cout << mouse->getString() << endl;
}
void visit(Keyboard *keyboard)
{
cout << keyboard->getString() << endl;
}
void visit(Monitor *monitor)
{
cout << monitor->getString() << endl;
}
};