Complete the code to declare the Visitor interface method for visiting an Element.
interface Visitor {
void visit([1] element);
}The Visitor interface defines a visit method that takes an Element object to perform operations on it.
Complete the code to implement the accept method in the Element class that accepts a Visitor.
class Element { void accept([1] visitor) { visitor.visit(this); } }
The accept method takes a Visitor object and calls its visit method passing itself.
Fix the error in the ConcreteVisitor class method signature for visiting an Element.
class ConcreteVisitor implements Visitor { public void visit([1] element) { // operation implementation } }
The ConcreteVisitor must implement the visit method with the Element parameter type as declared in Visitor interface.
Fill both blanks to complete the double dispatch call in the client code.
Element element = new ConcreteElement(); Visitor visitor = new ConcreteVisitor(); element.[1](visitor); visitor.[2](element);
The client calls accept on the element passing the visitor, then visitor calls visit on the element to perform operation.
Fill all three blanks to complete the Visitor pattern structure with multiple elements and visitors.
interface Visitor {
void visit([1] elementA);
void visit([2] elementB);
}
class ConcreteVisitor implements Visitor {
public void visit([3] elementA) {
// implementation
}
public void visit(ElementB elementB) {
// implementation
}
}The Visitor interface declares visit methods for each Element type. ConcreteVisitor implements these methods with matching parameter types.
