访问者模式通过双重分发在不修改元素类的前提下扩展操作,由抽象元素、具体元素、抽象访问者、具体访问者和对象结构组成,适用于元素稳定但操作多变的场景,如AST处理,优点是符合开闭原则,缺点是新增元素需修改所有访问者。

访问者模式(Visitor Pattern)是一种行为型设计模式,它允许你在不修改对象结构的前提下,为对象结构中的元素添加新的操作。C++中实现访问者模式的关键在于双重分发(Double Dispatch),通过虚函数机制实现动态调用。
访问者模式包含以下几个核心部分:
下面是一个简单的C++实现,模拟对不同形状进行“绘制”和“计算面积”的操作:
// 抽象访问者class ShapeVisitor;
立即学习“C++免费学习笔记(深入)”;
// 抽象元素class Shape {public:virtual ~Shape() = default;virtual void accept(ShapeVisitor& visitor) = 0;};
// 具体元素:圆形class Circle : public Shape {public:double radius;Circle(double r) : radius(r) {}
<strong>void accept(ShapeVisitor& visitor) override {</strong>
<strong>visitor.visit(*this);</strong>
<strong>}</strong>};
// 具体元素:矩形class Rectangle : public Shape {public:double width, height;Rectangle(double w, double h) : width(w), height(h) {}
<strong>void accept(ShapeVisitor& visitor) override {</strong>
<strong>visitor.visit(*this);</strong>
<strong>}</strong>};
// 抽象访问者class ShapeVisitor {public:virtual ~ShapeVisitor() = default;virtual void visit(Circle& circle) = 0;virtual void visit(Rectangle& rectangle) = 0;};
// 具体访问者:绘图class DrawVisitor : public ShapeVisitor {public:void visit(Circle& circle) override {std::cout << "绘制半径为 " << circle.radius << " 的圆形\n";}
<strong>void visit(Rectangle& rectangle) override {</strong>
<strong>std::cout << "绘制宽 " << rectangle.width << " 高 " << rectangle.height << " 的矩形\n";</strong>
<strong>}</strong>};
// 具体访问者:计算面积class AreaVisitor : public ShapeVisitor {public:double totalArea = 0.0;
<strong>void visit(Circle& circle) override {</strong>
<strong>double area = 3.14159 * circle.radius * circle.radius;</strong>
<strong>std::cout << "圆形面积: " << area << "\n";</strong>
<strong>totalArea += area;</strong>
<strong>}</strong>
<strong>void visit(Rectangle& rectangle) override {</strong>
<strong>double area = rectangle.width * rectangle.height;</strong>
<strong>std::cout << "矩形面积: " << area << "\n";</strong>
<strong>totalArea += area;</strong>
<strong>}</strong>};
// 使用示例int main() {std::vector<std::unique_ptr
<strong>DrawVisitor drawVisitor;</strong>
<strong>AreaVisitor areaVisitor;</strong>
<strong>for (auto& shape : shapes) {</strong>
<strong>shape->accept(drawVisitor);</strong>
<strong>}</strong>
<strong>for (auto& shape : shapes) {</strong>
<strong>shape->accept(areaVisitor);</strong>
<strong>}</strong>
<strong>std::cout << "总面积: " << areaVisitor.totalArea << "\n";</strong>
<strong>return 0;</strong>}
访问者模式适合以下情况:
优点:
缺点:
基本上就这些。访问者模式在编译器、AST处理、UI控件遍历等场景中较为常见,合理使用能提升系统扩展性。
以上就是C++如何实现一个访问者模式(Visitor Pattern)_C++设计模式与访问者实现的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号