title: Visitor pattern Usage
body: <p><a href="https://stackoverflow.com/questions/9239445/sample-of-using-visitor-patternbefore-and-after">https://stackoverflow.com/questions/9239445/sample-of-using-visitor-patternbefore-and-after</a></p>
  
  <p>Do I understand correctly main purposes of Visitor pattern?
  As I understand:</p>
  
  <p><strong>Before 1</strong></p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          List&lt;CompanyItem&gt; items = new ArrayList&lt;CompanyItem&gt;();
          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;
          }
      }
  }
  </code></pre>
  
  <p><strong>Before 2</strong></p>
  
  <pre><code>public class Main3 {
  
      public static void main(String[] args) {
          List&lt;CompanyItem&gt; items = new ArrayList&lt;CompanyItem&gt;();
          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;
          }
      }
  }
  </code></pre>
  
  <p><strong>After(with using of Visitor pattern ???)</strong></p>
  
  <pre><code>public class Main1 {
  
      public static void main(String[] args) {
          List&lt;CompanyItem&gt; items = new ArrayList&lt;CompanyItem&gt;();
          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()); 
          }
      }
  }
  </code></pre>
  
  <p>Am I right?</p>