Add example of visitor pattern in C++

This commit is contained in:
2021-05-11 20:56:05 -04:00
parent 53ee3df451
commit 8177d4c191
5 changed files with 132 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
#include "visitor.hpp"
/******************************************************************************/
// Concrete components
void Gear::accept(PartVisitor *v)
{
v->visit(this);
}
void Spring::accept(PartVisitor *v)
{
v->visit(this);
}
/******************************************************************************/
// Concrete visitors
void PartVisitor::visit(Gear *g)
{
std::cout << g->getName() << " is price " << g->getPrice() << " with radius of "
<< g->getRadius() << std::endl;
}
void PartVisitor::visit(Spring *g)
{
std::cout << g->getName() << " is price " << g->getPrice()
<< " with elasticity of " << g->getElasticity() << std::endl;
}