Questions tagged [visitor-pattern]
The visitor-pattern tag has no usage guidance.
42
questions
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 ...
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 ...
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 ...
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 (...
-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 ...
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 ...
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 ...
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 "...
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 ...
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;
...
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 ...
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,
...
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 ...
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 ...
-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 = $...
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. ...
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 ...
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 ...
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 ...
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 ...
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
...
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 ...
-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 ...
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 ...
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 ...
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 ...
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(...
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 = ...
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 ...
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 ...
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> ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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. ...