visitor-pattern

How to write Visitor Pattern for a Abstract Syntax Tree in C#?

一曲冷凌霜 提交于 2019-11-27 20:45:30
问题 I have to write a visitor pattern to navigate the AST. Can anyone tell me more how would I start writing it? As far as I understand, each Node in AST would have visit() method (?) that would somehow get called (from where?). That about concludes my understanding. To simplify everything, suppose I have nodes Root, Expression, Number, Op and the tree looks like this: Root | Op(+) / \ / \ Number(5) \ Op(*) / \ / \ / \ Number(2) Number(444) 回答1: Pattern visitor is a design pattern that allows you

How does double dispatch work in Visitor pattern?

末鹿安然 提交于 2019-11-27 20:36:22
问题 I was looking into other questions related to the visitor pattern but couldn't understand the implementation of double dispatch in visitor pattern. Please refer to the link Visitor Pattern How does double dispatch work in the Visitor pattern? 回答1: The element object's accept method receives a visitor object and it calls the visit method on the visitor object. As the visitor object has several visit methods, based on the element type the appropriate visit method is called. Here we have two

Using the Visitor Pattern with template derived classes

蹲街弑〆低调 提交于 2019-11-27 17:02:07
问题 I try to implement the Visitor pattern with templated derived classes I work with gcc 4.5 here is the VisitorTemplate.hpp, I specialized Derived in the class Visitor, but I'd like to be able to handle any type: edit : thanks to the suggestions of interjay, the code compiles and runs without errors now #ifndef VISITORTEMPLATE_HPP_ #define VISITORTEMPLATE_HPP_ #include <iostream> #include <string> using namespace std; template<class T> Derived; class Visitor { public: virtual void visit(Derived

Delphi Enterprise: how can I apply the Visitor Pattern without circular references?

99封情书 提交于 2019-11-27 14:32:29
With Delphi 2009 Enterprise I created code for the GoF Visitor Pattern in the model view, and separated the code in two units: one for the domain model classes, one for the visitor (because I might need other units for different visitor implementations, everything in one unit? ' Big ball of mud ' ahead!). unit VisitorUnit; interface uses ConcreteElementUnit; type IVisitor = interface; IElement = interface procedure Accept(AVisitor :IVisitor); end; IVisitor = interface procedure VisitTConcreteElement(AElement :TConcreteElement); end; TConcreteVisitor = class(TInterfacedObject, IVisitor) public

Delphi Enterprise: how can I apply the Visitor Pattern without circular references?

南楼画角 提交于 2019-11-26 16:48:04
问题 With Delphi 2009 Enterprise I created code for the GoF Visitor Pattern in the model view, and separated the code in two units: one for the domain model classes, one for the visitor (because I might need other units for different visitor implementations, everything in one unit? 'Big ball of mud' ahead!). unit VisitorUnit; interface uses ConcreteElementUnit; type IVisitor = interface; IElement = interface procedure Accept(AVisitor :IVisitor); end; IVisitor = interface procedure

When should I use the Visitor Design Pattern? [closed]

99封情书 提交于 2019-11-26 01:34:24
问题 I keep seeing references to the visitor pattern in blogs but I\'ve got to admit, I just don\'t get it. I read the wikipedia article for the pattern and I understand its mechanics but I\'m still confused as to when I\'d use it. As someone who just recently really got the decorator pattern and is now seeing uses for it absolutely everywhere I\'d like to be able to really understand intuitively this seemingly handy pattern as well. 回答1: I'm not very familiar with the Visitor pattern. Let's see