Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8121131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:23:02+00:00 2026-06-06T05:23:02+00:00

The Visitor pattern allows operations on objects to be written without extending the object

  • 0

The Visitor pattern allows operations on objects to be written without extending the object class. Sure. But why not just write a global function, or a static class, that manipulates my object collection from the outside? Basically, in a language like java, an accept() method is needed for technical reasons; but in a language where I can implement the same design without an accept() method, does the Visitor pattern become trivial?

Explanation: In the Visitor pattern, visitable classes (entities) have a method .accept() whose job is to call the visitor’s .visit() method on themselves. I can see the logic of the java examples: The visitor defines a different .visit(n) method for each visitable type n it supports, and the .accept() trick must be used to choose among them at runtime. But languages like python or php have dynamic typing and no method overloading. If I am a visitor I can call an entity method (e.g., .serialize()) without knowing the entity’s type or even the full signature of the method. (That’s the “double dispatch” issue, right?)

I know an accept method could pass protected data to the visitor, but what’s the point? If the data is exposed to the visitor classes, it is effectively part of the class interface since its details matter outside the class. Exposing private data never struck me as the point of the visitor pattern, anyway.

So it seems that in python, ruby or php I can implement a visitor-like class without an accept method in the visited object (and without reflection), right? If I can work with a family of heterogeneous objects and call their public methods without any cooperation from the “visited” class, does this still deserve to be called the “Visitor pattern”? Is there something to the essence of the pattern that I am missing, or does it just boil down to “write a new class that manipulates your objects from the outside to carry out an operation”?

PS. I’ve looked at plenty of discussion on SO and elsewhere, but could not find anything that addresses this question. Pointers welcome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T05:23:03+00:00Added an answer on June 6, 2026 at 5:23 am

    This answer is made with an ignorance of PHP etc but the Visitor needs typically to call more than just a single method (you mentioned “serialize”) on the entities. When the Visit() method is called on the concrete Visitor, the Visitor is capable of running distictly different code for each entity subtype. I don’t see how that is different from a dynamically-types language (though I’d love some feedback).

    Another nice benefit of Visitor is it provides a clean seperation of the code that is getting run on each entity from the code that enumerates the entities. This has saved me some serious code duplication in at least one large project.

    As an aside, I’ve used Visitor in languages that did not have method overloading. You just replace Visit(TypeN n) with VisitN(TypeN n).


    Follow up from comments.

    This is a visitor psuedo code, and I don’t know how I would so it without the cooperation of the visited object (at least without a switch block):

    abstract class ScriptCommand
    {
       void Accept(Visitor v);
    }
    
    abstract class MoveFileCommand
    {
       string TargetFile;
       string DestinationLocation;
    
       void Accept(Visitor v)
       {
          v.VisitMoveFileCmd(this);  // this line is important because it eliminates the switch on object type
       }
    }
    
    abstract class DeleteFileCommand
    {
       string TargetFile;
    
       void Accept(Visitor v)
       {
          v.VisitDeleteFileCmd(this); // this line is important because it eliminates the switch on object type
    
       }
    }
    
    // etc, many more commands
    
    abstract class CommandVisitor
    {
       void VisitMoveFileCmd(MoveFileCommand cmd);
       void VisitDeleteFileCmd(DeleteFileCommand cmd);
       // etc
    }
    
    // concrete implementation
    
    class PersistCommandVisitor() inherits CommandVisitor
    {
       void VisitMoveFileCmd(MoveFileCommand cmd)
       {
          // save the MoveFileCommand instance to a file stream or xml doc
          // this code is type-specific because each cmd subtype has vastly
          // different properties
       }
    
       void VisitDeleteFileCmd(DeleteFileCommand cmd)
       { 
          // save the DeleteFileCommand instance to a file stream or xml doc
          // this code is type-specific because each cmd subtype has vastly
          // different properties
       }
    
    }
    

    The visitor infrastructure allows the handling of a wide array of command subtypes with no select case, swithc, if else.

    In regards to the visitor handling the enumerating, I think you are limiting yourself like that. That’s not to say a cooperating class (an abstract VisitorEnumerator) can’t be involved.

    For example, note this visitor is unaware of the order of enumeration:

    class FindTextCommandVisitor() inherits CommandVisitor
    {
       string TextToFind;
       boolean TextFound = false;
    
       void VisitMoveFileCmd(MoveFileCommand cmd)
       {
          if (cmd.TargetFile.Contains(TextToFind) Or cmd.DestinationLocation.Contains(TextToFind))
             TextFound = true;
       }
    
    
       void VisitDeleteFileCmd(DeleteFileCommand cmd)
       { 
          // search DeleteFileCommand's properties
       }
    
    }
    

    And this allows it to be reused like this:

    ScriptCommand FindTextFromTop(string txt)
    {
       FindTextCommandVisitor v = new FindTextCommandVisitor();
       v.TextToFind = txt;
       for (int cmdNdx = 0; cmdNdx < CommandList.Length; cmdNdx++)
       {
          CommandList[cmdNdx].Accept(v);
          if (v.TextFound)
             return CommandList[cmdNdx];  // return the first item matching
       }
    }
    

    and the enumerate the opposite way with the same visitor:

    ScriptCommand FindTextFromBottom(string txt)
    {
       FindTextCommandVisitor v = new FindTextCommandVisitor();
       v.TextToFind = txt;
       for (int cmdNdx = CommandList.Length-1; cmdNdx >= 0; cmdNdx--)
       {
          CommandList[cmdNdx].Accept(v);
          if (v.TextFound)
             return CommandList[cmdNdx];  // return the first item matching
       }
    }
    

    In real code I would create a base class for the enumerator and then subclass it to handle the different enumeration scenarios, while passing in the concrete Visitor subclass to completely decouple them. Hopefully you can see the power of keeping the enumeration seperate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My collegue suggested me to write a visitor pattern to navigate the AST. Can
I'm trying to use the visitor pattern to serialize the contents of objects. However
i have wrote the visitor pattern as follow but i don't understand what is
In a typical implementation of the Visitor pattern, the class must account for all
I am trying to implement example of visitor pattern, but I have trouble with
I'm a little bit unfriendly with Visitor pattern, but I have a task that
I'm just trying to understand the main benefits of using the Visitor pattern. Here's
I was looking into other questions related to the visitor pattern but couldn't understand
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
I'm trying to use the Visitor Pattern and I have as follows: public class

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.