Visitor pattern

This commit is contained in:
Bassem Girgis
2018-03-30 11:59:39 -05:00
parent 1806d6a1f2
commit ef09730518
4 changed files with 164 additions and 0 deletions

22
src/behavioral/SConscript Normal file
View File

@@ -0,0 +1,22 @@
#
# This file is part of cpp-patterns.
#
# cpp-patterns is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cpp-patterns is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cpp-patterns. If not, see <http://www.gnu.org/licenses/>.
#
Import('env')
env.program(targetName='visitorEx',
sources=['visitor/visitor.cpp'],
localSharedLibs=[])

View File

@@ -0,0 +1,37 @@
/*
* This file is part of cpp-patterns.
*
* cpp-patterns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cpp-patterns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cpp-patterns. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SRC_BEHAVIORAL_VISITOR_IELEMENT_HPP_
#define SRC_BEHAVIORAL_VISITOR_IELEMENT_HPP_
#include "IVisitor.hpp"
namespace behavioral {
namespace visitor {
struct IElement {
virtual ~IElement() = default;
virtual void accept(IVisitor& visitor) = 0;
};
} // namespace visitor
} // namespace behavioral
#endif // SRC_BEHAVIORAL_VISITOR_IELEMENT_HPP_

View File

@@ -0,0 +1,39 @@
/*
* This file is part of cpp-patterns.
*
* cpp-patterns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cpp-patterns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cpp-patterns. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SRC_BEHAVIORAL_VISITOR_IVISITOR_HPP_
#define SRC_BEHAVIORAL_VISITOR_IVISITOR_HPP_
class ElementA;
class ElementB;
namespace behavioral {
namespace visitor {
struct IVisitor {
virtual ~IVisitor() = default;
virtual void visit(ElementA& element) = 0;
virtual void visit(ElementB& element) = 0;
};
} // namespace visitor
} // namespace behavioral
#endif // SRC_BEHAVIORAL_VISITOR_IVISITOR_HPP_

View File

@@ -0,0 +1,66 @@
/*
* This file is part of cpp-patterns.
*
* cpp-patterns is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* cpp-patterns is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with cpp-patterns. If not, see <http://www.gnu.org/licenses/>.
*/
#include "IElement.hpp"
#include "IVisitor.hpp"
#include <algorithm>
#include <iostream>
#include <memory>
#include <vector>
struct ElementA : public behavioral::visitor::IElement {
virtual void accept(behavioral::visitor::IVisitor& visitor) override
{
std::cout << "ElementA::accept\n";
visitor.visit(*this);
}
};
struct ElementB : public behavioral::visitor::IElement {
virtual void accept(behavioral::visitor::IVisitor& visitor) override
{
std::cout << "ElementB:accept\n";
visitor.visit(*this);
}
};
struct VisitorA : public behavioral::visitor::IVisitor {
virtual void visit(ElementA& element) override
{
std::cout << "VisitorA::visit(ElementA)\n";
}
virtual void visit(ElementB& element) override
{
std::cout << "VisitorA::visit(ElementB)\n";
}
};
int
main(int argc, char** argv)
{
VisitorA va;
std::vector<std::unique_ptr<behavioral::visitor::IElement>> a(3);
a[0].reset(new ElementA());
a[1].reset(new ElementB());
a[2].reset(new ElementA());
std::for_each(a.begin(), a.end(), [&va](const auto& e) { e->accept(va); });
}