访客模式用法

时间:2012-02-11 13:22:22

标签: java design-patterns

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

我是否正确理解了访客模式的主要目的? 据我所知:

在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;
        }
    }
}

在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;
        }
    }
}

之后(使用访客模式???)

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()); 
        }
    }
}

我是对的吗?

4 个答案:

答案 0 :(得分:8)

从技术上讲,该示例实现了访问者模式。但是这个例子没有提升访问者的优势。重点是:如果您希望在同一数据结构上运行多个独立算法,则实现访问者模式开销 - 而不改变数据结构。

为了增强你的榜样,我提出了这样的改变:用一个系统替换简单的奖励系统,在 all <之间分配固定的奖金(例如当年的100k $) / em>经理根据每个经理的一些奖励积分。如果有两个经理,一个有140个点,另外有60个点,那么第一个获得70k $,第二个获得30k $。

这允许您有多个访问者:

  • 总结所有经理人的所有奖励积分
  • 使用上一步的总和在经理之间分配奖金(100k $)。将此计算的个人奖金设置为Manager
  • 中的字段
  • 第三位访客(PaydayVisitor)打印出对员工,老板和经理的支票,并返还所有已付款项的总和。

编辑在代码中,这看起来像这样(仅为了简洁省略了getter / setter):

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;
        }
    }
}

还有什么要做:给每个项目一些可打印的名称,以便在advisePayment中使用。

答案 1 :(得分:1)

看起来很好。但是在这种情况下,我只使用没有任何模式的多态。只需让CompanyItem有一个TotalSalary函数,你就可以使用它。如果您有不同的访问者,访问者将非常有用。

答案 2 :(得分:1)

是的,这是访客模式的正确用法,因为它为每个类做了不同的事情。但请记住,当您使用访问者模式时,您应该涵盖实现相应界面的所有类。

答案 3 :(得分:1)

这取决于您是否有SalaryVisitor以外的其他类型的访客。如果你没有任何其他访问者,你应该避免访问者模式:这是一个非常“沉重”的模式,这似乎没有必要。

修改
我原本说我更喜欢之前2 ,但是根据AH的评论,也许你真的可以创建一个名为PaidByCompany的新子类(坏名称,但我现在想不出什么好的),它有方法getTotalCompensation()。 在1之前也可以正常工作。