Visitor pattern
This commit is contained in:
22
src/behavioral/SConscript
Normal file
22
src/behavioral/SConscript
Normal 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=[])
|
||||||
37
src/behavioral/visitor/IElement.hpp
Normal file
37
src/behavioral/visitor/IElement.hpp
Normal 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_
|
||||||
39
src/behavioral/visitor/IVisitor.hpp
Normal file
39
src/behavioral/visitor/IVisitor.hpp
Normal 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_
|
||||||
66
src/behavioral/visitor/visitor.cpp
Normal file
66
src/behavioral/visitor/visitor.cpp
Normal 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); });
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user