Java – why do we call acceptor Accept() instead of visitor Visit() to start visitor?

In Wikipedia sample and GOF books, the use of visitor mode is initiated by calling the accept method on a recipient But why? Why can't we start calling the visit method with the required acceptor as a parameter? We can still make visitor behavior dependent on two types – visitor and recipient (dual scheduling) – and we can eliminate redundant calls (in my opinion)

This is sample code to illustrate this:

public interface Visitor {
    void visit(AcceptorA acceptor);
    void visit(AcceptorB acceptor);
}

//
// Visitor which sings
// 
class SingingVisitor implements Visitor {
    public void visit(AcceptorA acceptor) {
        System.out.println("sing A");
    }

    public void visit(AcceptorB acceptor) {
        System.out.println("sing B");
    }
}


//
// Visitor which talks
// 
class TalkingVisitor implements Visitor {
    public void visit(AcceptorA acceptor) {
        System.out.println("talk A");
    }

    public void visit(AcceptorB acceptor) {
        System.out.println("talk B");
    }
}

//
// Acceptor subclasses
// 
class AcceptorA implements BaseAcceptor {
}

class AcceptorB implements BaseAcceptor {
}

//
// Launcher class
// 
class VisitorMain {
    public static void main(String[] args) {
        Visitor v = new TalkingVisitor();
        AcceptorA a = new AcceptorA();
        AcceptorB b = new AcceptorB();

        v.visit(a);
        v.visit(b);
        v = new SingingVisitor();
        v.visit(a);
        v.visit(b);
    }
}

Solution

consider:

class House implements HouseAcceptor {
    HouseAcceptor kitchen;
    HouseAcceptor livingRoom;

    void accept(HouseVisitor visitor) {
        visitor.visit(this);
        kitchen.accept(visitor);
        livingRoom.accept(visitor);
    }
}

class Kitchen implements HouseAcceptor {
    void accept(HouseVisitor visitor) {
        visitor.visit(this);
    }
}

class LivingRoom implements HouseAcceptor {
    void accept(HouseVisitor visitor) {
         visitor.visit(this);
    }
}

class SpeakingHouseVisitor implements HouseVisitor {
    void visit(HouseAcceptor acceptor) {
        System.out.println("Inside a HouseAcceptor");
    }

    void visit(House acceptor) {
        System.out.println("Inside a House");
    }

    void visit(Kitchen acceptor) {
        System.out.println("Inside a Kitchen");
    }

    void visit(LivingRoom acceptor) {
        System.out.println("Inside a LivingRoom");
    }
}

...
HouseAcceptor acceptor = new House();
HouseVisitor visitor = new SpeakingHouseVisitor();

...
// Doing it your way
visitor.visit(acceptor);
// Output: Inside a HouseAcceptor

// Doing it the right way
acceptor.accept(visitor);
// Output:
// Inside a House
// Inside a Kitchen
// Inside a LivingRoom

Note that if you execute in your own way, there will be no difference in the runtime type of the receiver: static types will be used By performing dual scheduling, you can ensure that two runtime types are used

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>