Skip to main content

Questions tagged [visitor-pattern]

Filter by
Sorted by
Tagged with
2 votes
5 answers
229 views

Does the Visitor Pattern necessitate traversing a structure of instances?

I have a class hierarchy of elements (more static) with different operations on them in another class hierarchy (more flexible). During execution there is exactly one element given, and based on it's ...
Philipp Murry's user avatar
1 vote
2 answers
292 views

Message Dispatching: If, Visitor, or something else

I often write code to implement the dispatching and handling of "messages." My requirements are usually: I should be able to add new types of messages AND/OR new types of handlers without ...
Patrick Wright's user avatar
3 votes
1 answer
560 views

How to implement the visitor pattern without inheritance

I write embedded firmware using C++. A common job for firmware is to "handle" different types of "messages" (e.g., in a communication protocol). The "C" way of doing ...
Patrick Wright's user avatar
0 votes
3 answers
949 views

Use of Visitor pattern rather than enums

Lets say that a enum exists: public enum SomeEnum { FirstValue, SecondValue } It's use is spread through the code and all sort of wonderful logic hangs off it. If we now add another entry (...
Postie's user avatar
  • 11
-1 votes
1 answer
293 views

Avoid use of the visitor pattern in this very common scenario

Let's assume we need to send a message, and to do so we would like our client to be concerned only with constructing the message (DTO) and using a facade service to send it. We already know that we ...
blow's user avatar
  • 109
1 vote
1 answer
435 views

Does every Visited class have to be aware of the Visitor interface?

I'm taking another look at the pros and cons of the Visitor design pattern for a talk. There's obviously the issue that the Visitor has to be aware of every class it has specific handling for, but ...
deworde's user avatar
  • 1,932
1 vote
1 answer
230 views

How to implement visitor if not all functions are supported for each Element?

I have two Visitor classes in my code Visitor1 and Visitor2 that execute functions on the classes ElementA and ElementB (both implement the interface Visitable that defines accept(Visitor)). I do not ...
sininen's user avatar
  • 129
1 vote
2 answers
777 views

Appropriate use for the visitor design pattern

Assume I have a factory which takes in a series of bytes and outputs a pointer to a newly-created abstract message. Now, I would like to define some extensible, manageable, and clean way to "...
Patrick Wright's user avatar
1 vote
1 answer
581 views

Is using lambdas and overload resolution a recommended way to write a visitor for a variant?

If I have a discriminated union and want to write a function piecewise, the following code works just fine, but is taking advantage of some fairly tricky (at least to me) stuff involving overload ...
Daniel McLaury's user avatar
2 votes
2 answers
2k views

Alternative to the Visitor Design Pattern

I have been trying to come up with a method to "serialize" various objects into various different formats. For example: class Shape { public: virtual std::string_view name() const = 0; ...
Patrick Wright's user avatar
3 votes
2 answers
694 views

How to use visitor pattern when objects change frequently

It is said that visitor pattern is applicable to problems where objects rarely change but we add actions on those objects more frequently. What if the objects are changing too though? For example we ...
Ehsan Poursaeed's user avatar
1 vote
1 answer
409 views

Rock Paper Scissors with Visitor Pattern

I've been going over the Visitor Pattern to try to understand it. Is this a correct implementation of the Visitor Pattern for RPS?? Here's the implementation code: public enum ResultEnum { WIN, ...
shulito's user avatar
  • 121
2 votes
1 answer
170 views

Loose coupling vs transparency in visitor pattern that visits composite

I have a composite object (I will call it A) that has fixed number of sons (I will call them A1 and A2) All of them have an Accept(Visitor) method. I have a GUI, A is the main window that consists of ...
Aviv Aviv's user avatar
  • 117
1 vote
0 answers
147 views

Visitor pattern with internal iterators

I'm trying to understand the following description of implementing the visitor pattern with an internal iterator. It is from the GoF's Design Pattern Elements of Reusable Object-Oriented Software book ...
Yang's user avatar
  • 119
-2 votes
1 answer
49 views

Inheritance and domain-specific logic loses its independency?

Im sorry in forward, Im sure I wont be able to express myself well. Its a game system: abstract class GameType { private $score; public __constructor($score) { $this->score = $...
John Smith's user avatar
31 votes
6 answers
10k views

Visitor Pattern: what's the point of the `accept` method?

I'm trying to fully understand the visitor pattern. What I've learnt so far (correct me if I'm wrong) is: It's about adding operations to classes, without modifying the source code of those classes. ...
bgusach's user avatar
  • 616
3 votes
1 answer
635 views

Does the visitor pattern prevent the need to modify existing code when adding new data types?

From what I understand, the visitor pattern is supposed to solve the expression problem (described here), where a program needs to support performing multiple operations on multiple types, ideally ...
DylanSp's user avatar
  • 275
0 votes
1 answer
903 views

Can't I just use interfaces to implement the visitor pattern?

Every example of the visitor pattern that I see uses abstract classes. One of the major drawbacks of the visitor pattern is the fact that each visitor must implement a .visit method for every acceptor ...
Most_Arduous_Journey's user avatar
1 vote
0 answers
1k views

Combination of visitor and iterator pattern

While studying the visitor design pattern i found this phrase: You can use Visitor along with Iterator to traverse a complex data structure and execute some operation over its elements, even if they ...
Zethel's user avatar
  • 27
4 votes
2 answers
3k views

Combining composite, decorator and visitor patterns

I am currently working on a project where I need to manipulate a tree-like data structure, which I implemented with a composite pattern. I want to be able to do different actions on this data ...
Antoine Boisier-Michaud's user avatar
2 votes
1 answer
135 views

Traversing over two similar object structures using a pattern

Let's say I have an Object Structure like this that I import data into from a source: Reporting: Body: ReportingEntity: DocSpec Reports[]: ConstEntities[] DocSpec ...
Tekay37's user avatar
  • 235
3 votes
1 answer
417 views

Circular dependency problem

"Single item in a set depends on the whole set. Set depends on that item." I'm creating a compiler (https://github.com/SuperJMN/Plotty). In the last stage, the Intermediate Code is converted to ...
SuperJMN's user avatar
  • 453
-2 votes
1 answer
76 views

Design help needed

I have a model call Drug class Drug{ String drugId; String name; String strength; } this interface takes a drugId and returns the Drug Model interface IProcessor{ getDrug(String drugId) } // easy ...
Prabhu shanmughapriyan's user avatar
2 votes
1 answer
2k views

Is it OK to use the Visitor pattern for queries?

On the net and in books you commonly see examples of the visitor pattern just using "commands" i.e. methods perform an action and have no return type. Is it OK to use this pattern for a query ...
Dib's user avatar
  • 153
1 vote
1 answer
801 views

Visitor pattern and collecting visited data

I have a few operations to make over many similar elements. I would like to collect data from each element firstly and next bind all the data to an object (binding is expensive operation so I need to ...
deem's user avatar
  • 207
6 votes
2 answers
925 views

SAX-like parser: what is this pattern called?

I wrote a parser for a certain type of binary file with recursive structure. I made its API to be similar to SAX, that is: the parser accepts an object of a specific interface, this interface has ...
liori's user avatar
  • 725
9 votes
3 answers
11k views

Understanding the need of Visitor Pattern

After seeing an article on visitor pattern, it is clear to me how it works. And I created a sample program for my understanding; main(){ SortingAlgorithm bubbleSort; : intList.sort(...
Amit Kumar Gupta's user avatar
10 votes
5 answers
6k views

Idiomatic pattern matching equivalent in Java

I'm building a simulator which parses some events from STDIN and "runs" them. My background is mostly functional programming these days, so it seemed natural to do something like this: data Event = ...
closeparen's user avatar
6 votes
1 answer
2k views

Visitor Pattern, replacing objects

I have a program that translates a DSL to C++, which uses a Visitor pattern on the intermediate representation. I quite often need to replace the currently processed node with one of a different type ...
Simon Richter's user avatar
8 votes
3 answers
2k views

Does the visitor pattern violate the Liskov Substitution Principle

When using the Visitor pattern, I have observed the following: The visitor has weak dependencies to concrete types (each visit method has the concrete element as a parameter or is a method that ...
Chad's user avatar
  • 307
5 votes
2 answers
574 views

Determine equality of a DAG

Given a fairly traditional node class (below), what's the best way to implement equality on a given graph? If our node looks like this public abstract class Node{ private final Set<Node> ...
Groostav's user avatar
  • 257
3 votes
3 answers
331 views

Which pattern can I use for doing computations involving similar yet different object subtypes?

How do I design code that has two qualities: uses similar but different subtypes of an object uses similar but different types of computation on the above objects Example I am refactoring this ...
Dennis's user avatar
  • 8,247
9 votes
4 answers
2k views

Is Visitor Pattern valid in this scenario?

The goal of my task is to design a small system which can run scheduled recurring tasks. A recurring task is something like "send an email to administrator every hour from 8:00 am to 5:00 pm, Monday ...
Sher10ck's user avatar
  • 231
6 votes
1 answer
6k views

AST processing and usefulness of visitor pattern

I know the visitor pattern is typically used to traverse a hierarchy of heterogeneous objects (inheriting a same abstract object) and dissociate the processing of these objects from the data within ...
matovitch's user avatar
  • 171
4 votes
2 answers
4k views

Traversing an AST using Visitors

I'm writing a compiler for a C-like language, and I'm looking for an elegant way to traverse my abstract syntax tree. I'm trying to implement the Visitor pattern, although I'm not convinced that I'm ...
user2844493's user avatar
2 votes
2 answers
554 views

Visitor only applicable when using the Composite pattern?

For a long time I've tried to get my head wrapped around the visitor pattern, and somehow this thing keeps being rather fuzzy to me. I'm currently under the impression it is only useful to apply ...
Jeroen De Dauw's user avatar
1 vote
2 answers
355 views

Validating objects with each other - Design Pattern needed

I am running a zoo application. My zoo includes an abstract class of 'animal', and several deriving classes - 'zebra', 'elephant', 'orangutan', 'baboon' and so on. Of each class I have several ...
Berlo's user avatar
  • 127
10 votes
2 answers
16k views

Design for a tree using a visitor pattern, how to implement different kinds of traversal?

I've been asked a theoretical design question with an eye upon the GoF patterns.: "Given a design for a tree using a standard visitor pattern, how would your design look to allow a user to choose ...
Sven's user avatar
  • 251
4 votes
4 answers
757 views

Visitor Pattern still useful in languages supporting class extensions?

Is the visitor pattern useful in languages supporting class extensions? Why bother implementing it, when you have open classes or can subclass. If you want new functionality for a class you could ...
Sven's user avatar
  • 251
26 votes
3 answers
26k views

Implementing the Visitor Pattern for an Abstract Syntax Tree

I'm in the process of creating my own programming language, which I do for learning purposes. I already wrote the lexer and a recursive descent parser for a subset of my language (I currently support ...
marco-fiset's user avatar
  • 8,761
12 votes
1 answer
5k views

Using visitor pattern with large object hierarchy

Context I've been using with a hierarchy of objects (an expression tree) a "pseudo" visitor pattern (pseudo, as in it does not use double dispatch) : public interface MyInterface { void ...
T. Fabre's user avatar
  • 295
1 vote
2 answers
1k views

Use of the Visitor pattern and possible over engineering

I am developing a small application in .NET and am thinking of using XML to save the object model. My first thought was to separate the model and the generation of XML by using the Visitor pattern. ...
Tony's user avatar
  • 359