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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T19:17:33+00:00 2026-05-29T19:17:33+00:00

https://stackoverflow.com/questions/9239445/sample-of-using-visitor-patternbefore-and-after Do I understand correctly main purposes of Visitor pattern? As I understand: Before

  • 0

https://stackoverflow.com/questions/9239445/sample-of-using-visitor-patternbefore-and-after

Do I understand correctly main purposes of Visitor pattern?
As I understand:

Before 1

public class Main {

    public static void main(String[] args) {
        List<CompanyItem> items = new ArrayList<CompanyItem>();
        items.add(new Employee(10));
        items.add(new Employee(10.6));
        items.add(new Employee(15.9));
        items.add(new Manager(20.1));
        items.add(new Boss(30));

        double totalSalary = 0;
        for(CompanyItem i:items){
            if (i instanceof Employee) {
                totalSalary += ((Employee) i).getSalary();
            } else if (i instanceof Manager) {
                totalSalary += ((Manager) i).getSalary();
                totalSalary += ((Manager) i).getBonusses();
            }else if (i instanceof Boss) {
                totalSalary += ((Boss) i).getSalary();
                totalSalary += ((Boss) i).getAdditionalSalary();
            }
        }
        System.out.println(totalSalary);
    }

    interface CompanyItem {
    }

    static class Employee implements CompanyItem {
        double salary;

        public Employee(double salary) {
            this.salary = salary;
        }

        public double getSalary() {
            return salary;
        }
    }

    static class Manager implements CompanyItem {
        double salary, bonusses;

        public Manager(double salary) {
            this.salary = salary;
            this.bonusses = 1.5 * salary;
        }

        public double getSalary() {
            return  salary;
        }

        public double getBonusses() {
            return bonusses;
        }
    }

    static class Boss implements CompanyItem {
        double salary, addSalary;

        public Boss(double salary) {
            this.salary = salary;
            this.addSalary = 3 * salary;
        }

        public double getSalary() {
            return salary;
        }

        public double getAdditionalSalary() {
            return addSalary;
        }
    }
}

Before 2

public class Main3 {

    public static void main(String[] args) {
        List<CompanyItem> items = new ArrayList<CompanyItem>();
        items.add(new Employee(10));
        items.add(new Employee(10.6));
        items.add(new Employee(15.9));
        items.add(new Manager(20.1));
        items.add(new Boss(30));

        double totalSalary = 0;
        for(CompanyItem i:items){
            totalSalary+=i.getSalary();
            totalSalary+=i.getBonusses();
            totalSalary+=i.getAdditionalSalary();
        }
        System.out.println(totalSalary);
    }

    interface CompanyItem {
        public double getSalary();
        public double getBonusses();
        public double getAdditionalSalary();
    }

    static class Employee implements CompanyItem {
        double salary;

        public Employee(double salary) {
            this.salary = salary;
        }

        public double getSalary() {
            return salary;
        }

        @Override
        public double getBonusses() {
            return 0;
        }

        @Override
        public double getAdditionalSalary() {
            return 0;
        }
    }

    static class Manager implements CompanyItem {
        double salary, bonusses;

        public Manager(double salary) {
            this.salary = salary;
            this.bonusses = 1.5 * salary;
        }

        public double getSalary() {
            return  salary;
        }

        public double getBonusses() {
            return bonusses;
        }

        @Override
        public double getAdditionalSalary() {
            return 0;
        }
    }

    static class Boss implements CompanyItem {
        double salary, addSalary;

        public Boss(double salary) {
            this.salary = salary;
            this.addSalary = 3 * salary;
        }

        public double getSalary() {
            return salary;
        }

        public double getAdditionalSalary() {
            return addSalary;
        }

        @Override
        public double getBonusses() {
            return 0;
        }
    }
}

After(with using of Visitor pattern ???)

public class Main1 {

    public static void main(String[] args) {
        List<CompanyItem> items = new ArrayList<CompanyItem>();
        items.add(new Employee(10));
        items.add(new Employee(10.6));
        items.add(new Employee(15.9));
        items.add(new Manager(20.1));
        items.add(new Boss(30));

        SalaryVisitor visitor = new SalaryVisitor();
        for(CompanyItem i:items){
            i.accept(visitor);
        }
        System.out.println(visitor.getTotalSalary());
    }

     interface CompanyItem {
        public void accept(Visitor v);
    }

    static class Employee implements CompanyItem {
        double salary;

        public Employee(double salary) {
            this.salary = salary;
        }

        public double getSalary() {
            return salary;
        }

        @Override
        public void accept(Visitor v) {
            v.visit(this);
        }
    }

    static class Manager implements CompanyItem {
        double salary,bonusses;

        public Manager(double salary) {
            this.salary = salary;
            this.bonusses = 1.5 * salary;
        }

        public double getSalary() {
            return  salary;
        }

        public double getBonusses(){
            return bonusses;
        }

        @Override
        public void accept(Visitor v) {
            v.visit(this);
        }
    }

    static class Boss implements CompanyItem {
        double salary, addSalary;

        public Boss(double salary) {
            this.salary = salary;
            this.addSalary = 3 * salary;
        }

        public double getSalary() {
            return  salary;
        }
        public double getAdditionalSalary(){
            return addSalary;
        }

        @Override
        public void accept(Visitor v) {
            v.visit(this);
        }
    }

    interface Visitor {
        public void visit(Employee e);
        public void visit(Manager m);
        public void visit(Boss b);
    }

    static class SalaryVisitor implements Visitor {
        double totalSalary;

        public SalaryVisitor() {
            totalSalary = 0;
        }

        public double getTotalSalary(){
            return totalSalary;
        }

        @Override
        public void visit(Employee e) {
            totalSalary += e.getSalary();           
        }

        @Override
        public void visit(Manager m) {
            totalSalary += (m.getSalary()+m.getBonusses()); 
        }

        @Override
        public void visit(Boss b) {
            totalSalary += (b.getSalary()+b.getAdditionalSalary()); 
        }
    }
}

Am I right?

  • 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-29T19:17:34+00:00Added an answer on May 29, 2026 at 7:17 pm

    Technically the example implements the visitor pattern just fine. But the example does not promote the advantage(s) of a visitor. The main point is: Implement the visitor pattern overhead if you expect several independent algorithms working on the same data structure – without changing the structure of the data.

    In order to enhance your example I propose this changes: Replace the simple bonus system by a system where a fixed bonus (e.g. 100k$ in the current year) is distributed between all managers according to some bonus points each manager has. If there are two managers, one has 140 points, the other 60 points, then the first one gets 70k$, the second 30k$.

    This allows you to have several visitors:

    • One to sum up all bonus points of all Managers
    • One to distribute the bonus (100k$) between the managers using the sum from the previous step. Set this calculated individual bonus into a field in Manager
    • A third visitor (PaydayVisitor) prints out the checks for employees, bosses and managers and also returns a sum of all payments done.

    EDIT In code this would look like this (getter/setter omitted for brevity only):

    import java.util.ArrayList;
    import java.util.List;
    
    public class VisitorExample {
        public static void main(String[] args) {
            List<CompanyItem> items = new ArrayList<CompanyItem>();
            items.add(new Employee(10));
            items.add(new Employee(10.6));
            items.add(new Employee(15.9));
            items.add(new Manager(20.1, 140));
            items.add(new Manager(42.1, 70));
            items.add(new Boss(30, 10));
    
            // sum up all bonus points of all Managers
            BonusPointVisitor bonusPointVisitor = new BonusPointVisitor();
            for(CompanyItem i: items)
                i.accept(bonusPointVisitor);
    
            // distribute given bonus sum among the managers
            BonusDistributorVisitor bonusDistributorVisitor = 
                new BonusDistributorVisitor(bonusPointVisitor.totalBonusPoints, 100.0);
            for(CompanyItem i: items)
                i.accept(bonusDistributorVisitor);
    
            // PayDay - print all checks
            PrintCheckVisitor printCheckVisitor = new PrintCheckVisitor();
            for(CompanyItem i: items)
                i.accept(printCheckVisitor);
            System.out.println("total money spent this month: "+printCheckVisitor.totalPayments);
        }
    
        interface CompanyItem {
            public void accept(Visitor v);
        }
    
        interface Visitor {
            public void visit(Employee e);
            public void visit(Manager m);
            public void visit(Boss b);
        }
    
        static class Employee implements CompanyItem {
            double salary;
    
            public Employee(double salary) {
                this.salary = salary;
            }
    
            @Override
            public void accept(Visitor v) {
                v.visit(this);
            }
        }
    
        static class Manager implements CompanyItem {
            double salary, bonusPoints, bonus;
    
            public Manager(double salary, double bonusPoints) {
                this.salary = salary;
                this.bonusPoints = bonusPoints;
                this.bonus = 0;
            }
    
            @Override
            public void accept(Visitor v) {
                v.visit(this);
            }
        }
    
        static class Boss implements CompanyItem {
            double salary, addSalary;
    
            public Boss(double salary, double addSalary) {
                this.salary = salary;
                this.addSalary = addSalary;
            }
    
            @Override
            public void accept(Visitor v) {
                v.visit(this);
            }
        }
    
        static class BonusPointVisitor implements Visitor {
            double totalBonusPoints = 0d;
    
            @Override
            public void visit(Employee e) {
            }
    
            @Override
            public void visit(Manager m) {
                totalBonusPoints += m.bonusPoints;
            }
    
            @Override
            public void visit(Boss b) {
            }
        }
    
    
        static class BonusDistributorVisitor  implements Visitor {
            double totalBonusPoints, totalBonus;
    
            public BonusDistributorVisitor(double totalBonusPoints, double totalBonus) {
                this.totalBonusPoints = totalBonusPoints;
                this.totalBonus = totalBonus;
            }
    
            @Override
            public void visit(Employee e) {
            }
    
            @Override
            public void visit(Manager m) {
                m.bonus = (m.bonusPoints / totalBonusPoints) * totalBonus;
            }
    
            @Override
            public void visit(Boss b) {
            }
        }
    
        static class PrintCheckVisitor implements Visitor {
            double totalPayments = 0;
    
            @Override
            public void visit(Employee e) {
                advisePayment(e.salary);
            }
    
            @Override
            public void visit(Manager m) {
                advisePayment(m.salary + m.bonus);
            }
    
            @Override
            public void visit(Boss b) {
                advisePayment(b.salary + b.addSalary);
            }
    
            private void advisePayment(double amount){
                System.out.println("pay "+amount+" credits");
                totalPayments += amount;
            }
        }
    }
    

    What is left to be done: Give each item some printable name for usage in advisePayment.

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

Sidebar

Related Questions

Duplicate: https://stackoverflow.com/questions/587676/why-do-programs-in-unix-like-environments-have-numbers-after-their-name/ For instance, if I type: man ps ...and then scroll to the
Duplicate of https://stackoverflow.com/questions/885696/how-do-i-perform-a-better-colorize-function I am using this function in vb2005 to colorize a pixel,
After reading a StackoOverflow question https://stackoverflow.com/questions/182112/funny-loading-statements-to-keep-users-amused , I was really intrigued to ponder upon
After these questions: https://stackoverflow.com/questions/8589315/jsf2-dynamic-template Dynamic ui:include How can I retrieve an object on @WindowScoped?
Duplicate : https://stackoverflow.com/questions/135651/learning-unit-testing I'm trying to develop some software for my research group to
Related to https://stackoverflow.com/questions/139944/where-can-one-find-free-software-icons-images I have a need for free weather-related icons. Specifically, I need
In answering this question ( https://stackoverflow.com/questions/352317/c-coding-question#352327 ), it got me wondering... Is there any
I posted this question: https://stackoverflow.com/questions/418597/java-and-net-for-php-programmer and the answers I was given didn't really help
Referring to this question: https://stackoverflow.com/questions/2035449/why-is-oop-hard-for-me class Form { protected $inputs = array(); public function
I was reading this thread/post: https://stackoverflow.com/questions/262298/windows-c-ui-technology and am also wondering about a non .NET

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.