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 9239339
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T07:57:54+00:00 2026-06-18T07:57:54+00:00

From the Visitor pattern chapter of Gof book on Design patterns : Accumulating state

  • 0

From the Visitor pattern chapter of Gof book on Design patterns :

Accumulating state. Visitors can accumulate state as they visit each element in the object structure.Without a visitor,this state
would be passed as extra arguments to the operations that perform the
traversal,or they might appear as global variables.

What do the authors mean by accumulating state here and how does accumulating state help ? Please give a practical example where accumulating state during the traversal process is helpful.

  • 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-18T07:57:55+00:00Added an answer on June 18, 2026 at 7:57 am

    You can add fields to the visitor class and store state there.

    Example:

    class MyVisitor implements Visitor {
    
      private final List<Object> seenObjects = new ArrayList<Object>();
    
      void visitFoo(Foo f) {
        seenObjects.add(f);
      }
    
      void visitBar(Bar b) {
        seenObjects.add(b);
      }
    }
    

    I wouldn’t say that storing state in the visitor “helps”. Either you need the state to perform the operation, then you need to put it there, or you don’t, then please don’t store it.

    Without a visitor the code would usually look like this:

    class MyHandler {
    
      static void handle(Object o, List<Object> seenObjects) {
        seenObjects.add(o);
        if (o instanceof Foo) {
          // ...
        } else if (o instanceof Bar) {
          // ...
        }
        // possibly a recursive call to handle() somewhere, passing the seenObjects list
      }
    }
    

    The alternative would be to store the state in a field in the class, just like in the visitor case. This is also possible here, however, I would say that putting the state in fields is less common when not using the visitor pattern (because it’s not necessary, and it is usually a good idea to try avoiding mutable state in fields).

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

Sidebar

Related Questions

The visitor design pattern is a way of separating an algorithm from an object
I have a C++ state machine implemented using the State design pattern. Each state
Why can't I pattern match against Node[T] ? object Visitor { def inorder[T](root: Node[T])
how can I get all the unique visitor from the GAPI class. $ga =
In this question it is said I can use visitor pattern instead of a
I want to identify from where a visitor coming to my webpage.Is there anyways
How should I modify the bundled properties of a vertex from inside a visitor?
Is there a way to cache pages from previous visitors and then share that
I redirect the visitors in my website from page A to page B. In
From this context: import itertools lines = itertools.cycle(open('filename')) I'm wondering how I can implement

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.