访客模式 + 开闭原理
是否可以实现尊重 访客模式 wikipedia.org/wiki/Open/close_principle" rel="noreferrer">开放/封闭原则,但仍然能够添加新的可访问类?
开放/封闭原则指出“软件实体(类、模块、函数等)应该对扩展开放,但对修改关闭”。
struct ConcreteVisitable1;
struct ConcreteVisitable2;
struct AbstractVisitor
{
virtual void visit(ConcreteVisitable1& concrete1) = 0;
virtual void visit(ConcreteVisitable2& concrete2) = 0;
};
struct AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor) = 0;
};
struct ConcreteVisitable1 : AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor)
{
visitor.visit(*this);
}
};
struct ConcreteVisitable2 : AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor)
{
visitor.visit(*this);
}
};
您可以实现任意数量的从 AbstractVisitor 派生的类:它是开放扩展的。 您无法添加新的可访问类,因为从 AbstractVisitor 派生的类将无法编译:它已关闭以进行修改。
AbstractVisitor 类树遵循开闭原则。 AbstractVisitable 类树不遵循开闭原则,因为它无法扩展。
除了如下扩展 AbstractVisitor 和 AbstractVisitable 之外,还有其他解决方案吗?
struct ConcreteVisitable3;
struct AbstractVisitor2 : AbstractVisitor
{
virtual void visit(ConcreteVisitable3& concrete3) = 0;
};
struct AbstractVisitable2 : AbstractVisitable
{
virtual void accept(AbstractVisitor2& visitor) = 0;
};
struct ConcreteVisitable3 : AbstractVisitable2
{
virtual void accept(AbstractVisitor2& visitor)
{
visitor.visit(*this);
}
};
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle, but still be able to add new visitable classes?
The Open/Closed Principle states that "software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification".
struct ConcreteVisitable1;
struct ConcreteVisitable2;
struct AbstractVisitor
{
virtual void visit(ConcreteVisitable1& concrete1) = 0;
virtual void visit(ConcreteVisitable2& concrete2) = 0;
};
struct AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor) = 0;
};
struct ConcreteVisitable1 : AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor)
{
visitor.visit(*this);
}
};
struct ConcreteVisitable2 : AbstractVisitable
{
virtual void accept(AbstractVisitor& visitor)
{
visitor.visit(*this);
}
};
You can implement any number of classes which derives from AbstractVisitor: It is open for extension. You cannot add a new visitable class as the classes derived from AbstractVisitor will not compile: It closed for modification.
The AbstractVisitor class tree respects the Open/Closed Principle.
The AbstractVisitable class tree does not respect the Open/Closed Principle, as it cannot be extended.
Is there any other solution than to extend the AbstractVisitor and AbstractVisitable as below?
struct ConcreteVisitable3;
struct AbstractVisitor2 : AbstractVisitor
{
virtual void visit(ConcreteVisitable3& concrete3) = 0;
};
struct AbstractVisitable2 : AbstractVisitable
{
virtual void accept(AbstractVisitor2& visitor) = 0;
};
struct ConcreteVisitable3 : AbstractVisitable2
{
virtual void accept(AbstractVisitor2& visitor)
{
visitor.visit(*this);
}
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C++ 中,非循环访问者 (pdf) 可以让您得到您想要的想。
In C++, Acyclic Visitor (pdf) gets you what you want.
您可能想查看有关“表达式问题”的研究,请参阅
http://lambda- the-ultimate.org/node/2232
我认为这个问题主要是学术性的,但它已经被研究了很多,所以你可以阅读一些关于用现有语言或实现它的不同方法的东西具有各种语言扩展。
You might want to check out research on "the expression problem", see e.g.
http://lambda-the-ultimate.org/node/2232
I think the problem is mostly academic, but it's something that has been studied a lot, so there's a bit of stuff you can read about different ways to implement it in existing languages or with various language extensions.