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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:17:19+00:00 2026-05-24T13:17:19+00:00

The visitor design pattern is a way of separating an algorithm from an object

  • 0

The visitor design pattern is a way of separating an algorithm from an object structure it operates on. That is official definition of it. I am trying to figure out how this doesn’t break encapsulation. If say for example I have different types of classes for different types of bank accounts [Saving/Fixed/Current] implementing abstract class Account, should I put the calculate interest method as an abstract method in the abstract Account class or do I send the account type to the Visitor implementation and calculate it there?

Method 1: Should the Visitor implementation be responsible for calculating interest for the different account types?

public interface IInterestVisitor
{
    void GetInterest(Savings AccountType);
    void GetInterest(Fixed AccountType);
    void GetInterest(Current AccountType);
}

Method 2: Or should the Account class’s implementers do it?

public abstract class Account
{
    public abstract void AcceptCalculateInterestVisitor(IInterestVisitor iv);
    public abstract int CalculateInterestAmount();
}

If I use method 1, which is the visitor implementation that implements IInterestVisitor as above, then the job of calculating interest will be delegated to the visitor class. Using this approach, if I add another account type, then I will need to modify the visitor implementation everytime a new account comes along.

However, if I leave the interest calculation bit to the abstract Account classes implementations as above in method 2, then in my opinion [correct me if I am wrong here] I am not breaking encapsulation. In addition there is less code to modify as all I do is add a new class and have the visitor implement an interface like the one below

public interface IInterestVisitor
{
    void GetInterest(Account AccountType);
}


public class InterestVisitor : IInterestVisitor
{
    public void GetInterest(Account AccountType)
    {
        int i = AccountType.CalculateInterestAmount();
        Console.WriteLine(i);
    }
}

As you can see, there is no modification needed for the interest visitor class using method 2. Does method 1 break encapsulation? Can method 2 still be called a visitor pattern?

Thanks for reading…

  • 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-05-24T13:17:20+00:00Added an answer on May 24, 2026 at 1:17 pm

    Visitor lets you define a new operation without changing the classes
    of the elements on which it operates.

    In code provided I don’t see that you need to change an object in order to fit your needs, what Visitor basically does, is change object state by using provided Properties/Methods/Fields of object itself.

    So, by me your code can fit Visitor pattern, if that was actually question, also cause patterns are guidelines and not rigid rules.

    I personally would chose second way, as it much more clear and OOP oriented.

    Regards.

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

Sidebar

Related Questions

What is the difference between applying the visitor design pattern to your code and
I'm trying to use the Visitor Pattern and I have as follows: public class
I need to write a program implementing the visitor design pattern. The problem is
I am attempting to implement the Visitor Design Pattern using OCaml's OO constructs and
I have an object, based on the Singleton design, that I use for user
Is the Visitor Pattern the fastest way to accomplish method parameter type identification (effectively
I'm struggling with applying the visitor pattern on some objects that have scalar members
Is it possible to implement the Visitor Pattern respecting the Open/Closed Principle , but
What's the best way to handle a visitor constructing their own URL and replacing
I would like to be able to detect what country a visitor is from

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.